我有一些模型文件中的函數:返回承諾在的NodeJS未定義
Group.getGroupsOtherThanSelectedGroupAndItsDescendents(groupId, 100).then(function(selectedGroups) {
console.log(selectedGroups);
});
但始終,我得到selectedGroups:
module.exports.getGroupsOtherThanSelectedGroupAndItsDescendents = wrap(function*(topGroupId, generations) {
const topGroup = yield Group.find({parent: topGroupId}).populate('parent').populate('effect').populate('nature');
let groups = [];
let ids = [topGroupId];
for(let i = 0; i < generations; i++) {
const thisLevelGroups = yield Group.find({ parent : { $in : ids } }).populate('parent').populate('effect').populate('nature');
ids = thisLevelGroups.map(group => group._id);
groups = groups.concat(thisLevelGroups);
}
Group.getAllGroups(function(err, allGroups) {
groups.forEach(function(group) {
var index = allGroups.map(function(thisGroup) { return thisGroup.name; }).indexOf(group.name);
allGroups.splice(index, 1);
}, this);
return allGroups;
});
});
現在從另一個文件如下,我調用這個函數未定義。
我認爲這個問題是:
allGroups之前從異步方法稱爲getAllGroups返回,返回selectedGroups的價值。所以它是不確定的。
但我不知道如何解決這個問題。
你是否在函數getGroupsOtherThanSelectedGroupAndItsDescendents中返回了一些東西? – caisah
男人,這是一個長功能名稱。 – gyre
@gyre你能建議我小一點嗎? – Vishal