0
を使用してお約束します。続きNode.jsの連鎖は、私は約束のためのMongoDBとq.jsでのNode.jsを使用していますq.js
は私のMongoDBスキーマです:
{
UserId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
FirstName: String,
LastName: String,
Gender: String,
userDocument: [userDocumentSchema],
userEducation: [userEducationDetailsSchema]
};
var userEducationDetailsSchema = new Schema({
DegreeName: String,
University: String,
});
var userDocumentSchema = new mongoose.Schema({
DocumentName: String,
DocumentPath: String
});
I have following function by chaining promises:
var updateUserDetails = function (req, res) {
var finalResult = [];
/* This is the function to update user using promise.*/
updateUserBasicDetails(req).then(function (req) {
_.each(req.body.userEducation, function (sd) {
return getEducationDetail(req.body._id, sd) /* this fun finds if education details are present or not by iterating usertEducation Array */
.then(function (result) {
if (result) {
/* if education details exist then this will update the details */
return updateEducationDetails(sd, req.body._id).then(
function (result) {
return result;
})
} else {
/*else add the education details*/
return addEducationDetails(sd, req.body._id).then(
function (result) {
return result;
})
}
}).then(function (result) {
finalResult.push(result);
})
})
return JSON.stringify(finalResult);
}).then(function (finalResult) {
res.json({
"token": finalResult /*but here m getting empty result */
})
}).catch(console.log).done();
}
私のクエリは次のとおりです。これは約束のchaingingを実装するための正しい方法
- ですか?
- 最後のチェーンでは空の結果が出ますが、コンソールにo/pを印刷すると正しい結果が得られます。
- 私はgetEducationDetail機能の反復を行っている方法、それが正しい方法であるか、いずれかの選択肢があります。もしそうなら、どうすれば同じことができますか?
のあなたが同期 'finalResult'を返すが、値されている矢印の機能を使用することができます非同期にのみ読み込まれます。 – thefourtheye