currentRole.id is a promise, so you can call then() on it to wait for its resolution:
var currentRole = new Role(myRole, comId); currentRole.id.then(function (result) {
This seems like a weird API, but you expect your object to be "ready to use" when its constructor returns. It is better to have the getId promise return function on a Role prototype, so that you do something like this instead:
var currentRole = new Role(myRole, comId); currentRole.getId().then(function (result) {
You should also consider handling this error in order to reject a promise:
var getId = function (name, companyId) { return new Promise(function(resolve, reject) { Roles.findOne({companyId:companyId, name:name}, function(err,result) { if (err) { return reject(err); } resolve(result._id); }); }); };
and add a rejection handler to your getId call:
var currentRole = new Role(myRole, comId); currentRole.getId().then(function (result) {
or equivalently:
var currentRole = new Role(myRole, comId); currentRole.getId().then(function (result) {
source share