I'm using Sequelize
database orm
and by this code as
model.create(
{
username: 'John'
} ).then( function (result) {
return result.userUniqueId;
} );
I can create new user in database and i can print userUniqueId
inside of then()
.
after create user I want to use returned userUniqueId
in other functions or only print that on console, but i can't return result.userUniqueId
from that, for resolve this problem I'm trying to use Promise
to get result then I deployed that to:
'use strict';
module.exports = {
createNewUser: function (model) {
return new Promise(
function (resolve, reject) {
model.create(
{
username: 'John'
} ).then( function (result) {
return result.userUniqueId;
} );
} )
.then( function (result) {
resolve( result.userUniqueId );
} );
},
};
but i get this result outside of createNewUser
function as console.log( controller.createNewUser( models.users ) );
Result:
Promise { <pending> }
How can i resolve this problem?
UPDATE:
'use strict';
module.exports = {
createNewUser: function (model) {
return new Promise((resolve, reject) => {
return model.create(
{
username: 'John'
})
.then(function (result) {
return result.userUniqueId;
})
.then(value => resolve(value));
});
},
};
and
var userId = controller.createNewUser(models.users)
.then(function (userId) {
return userId;
});
console.log(userId);
print Promise object