mongoose承诺不传递数据到下一个链

我用promise来查询MongoDB。 结果只能在第一个.then(function(results){ // can send the result from here..}) 。 但是,当我操纵结果,并将其传递给下一个.then()链,它是不可访问的。 以下是完整的function。

 exports.getAcl = function(req, res) { User.findAsync({}, { acl: 1 }) .then(function(results){ var aclList = []; results.forEach(function(result,index,arr){ aclList[result._id] = result; if (index === (arr.length - 1)) { console.log('I can log the aclList here..', aclList) return aclList // But neither able to send it to next chain nor to front end res.send(aclList) } }) }) .then(function(aclList){ console.log(aclList) // Loging undefined res.status(200).json(aclList); // undefined }) .catch(handleError(res)); } 

请让我知道我在这里做错了什么..谢谢

代替

 results.forEach(function(result,index,arr){ aclList[result._id] = result; if (index === (arr.length - 1)) { console.log('I can log the aclList here..', aclList) return aclList // But neither able to send it to next chain nor to front end res.send(aclList) } }) 

尝试

 var resArray = []; results.forEach(function(result,index){ aclList[result._id] = result; if (index === (arr.length - 1)) { newRes.push(aclList); } }) // console.log(resArray) to verify they are there return resArray; 

底线:不要使用多个返回每个function(如forEach )。