我有一個函數,列出所有用戶和角色refrenced組。現在我有其他的功能,採取用戶角色refid並返回組名稱。在嘗試返回姓名時,我收到了待處理狀態。MEAN節點js承諾返回undefinded
function getAll() {
var deferred = Q.defer();
db.users.find().toArray(function(err, users) {
if (err) deferred.reject(err.name + ': ' + err.message);
// return users (without hashed passwords)
users = _.map(users, function(user) {
//console.log(user);
return _.omit(user, ['hash']);
});
users = _.map(users, function(user){
refId = {}= user['role'][0]['oid']['_id'];
//console.log(typeof refId);
user = _.omit(user, ['role']);
user.role = userRole.userRole(refId).then(function(err,rid){
if(err){
deferred.reject(err.name+':'+err.message);
}
deferred.resolve();
console.log(deferred.resolve(rid));
return deferred.promise;
console.log(deferred.promise);
});
return user;
//console.log(user);
})
// getRefId(users)
//console.log(users);
deferred.resolve(users);
});
function userRole(rid){
return new Promise((resolve, reject) => {
db.groups.findOne({"_id":rid}, function(err, doc){
if(err){
reject(err.name + ':' + err.message);
}
if(doc){
resolve({"name": doc.name});
//console.log(doc.name);
}
})
})
}
HI其實我使用mongoskin。仍在掙扎 –