Javascript Mongoose数组返回空?
我试图将mongoose Id添加到我的2个数组中,但它返回为空数组。 我似乎找不到问题所在这是我的功能Javascript Mongoose数组返回空?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我试图将mongoose Id添加到我的2个数组中,但它返回为空数组。 我似乎找不到问题所在这是我的功能 exports.create = (body) => { console.log(body.projectName); const theDate = getDate(); qpTemplate.findOne().sort({version: -1}).exec(function(err, doc) { var answerArray = []
exports.create = (body) => {
console.log(body.projectName);
const theDate = getDate();
qpTemplate.findOne().sort({version: -1}).exec(function(err, doc) {
var answerArray = [];
var questionArray = [];
var newProject = new projectModel({
_id: id(),
owner: Id,
projectName: body.projectName,
date: theDate,
version: 1.0,
});
var qp = new questionPackageModel ({
_id: id(),
version: 1,
questionIds: [], // this one i want to populate
projectId: newProject._id
});
console.log("hej")
doc.questionIds.map(theId => {
questionTemplate.findById(theId, function (err, question) {
var theQuestion = new questionModel({
_id: id(),
qpId: qp._id,
categoryId: question.categoryId,
order: question.order,
version: question.version,
question: question.question,
answerIds: [], // this one i want to populate
name: question.name,
legacyName: question.legacyName,
description: question.description
})
question.answerIds.map(answerId => {
answerTemplate.findById(answerId, function (err, answer) {
var theAnswer = new answerModel({
_id: id(),
questionId: theQuestion._id,
name: answer.name,
order: answer.order,
answerText: answer.answerText,
value: answer.value,
placeholder: answer.placeholder,
settings:answer.settings,
description: answer.description
})
theQuestion.answerIds.push(theAnswer._id); // returns an empty array at the end
answerArray.push(theAnswer);
theAnswer.save();
});
})
qp.questionIds.push(theQuestion._id); // returns an empty array in the end
questionArray.push(theQuestion);
theQuestion.save()
});
})
newProject.qpId = qp._id;
qp.save();
newProject.save();
console.log(questionArray);
console.log(newProject)
return(items={answerArray,questionArray,qp,newProject})
})
}
我试图实现的是用模型的id相互连接,这就是为什么我要将模型的id添加到阵列中。我不想把整个对象都放在那里,因为我正在将这些数据推送到需要平面状态的redux客户端
**我感谢每一个答案** 主要问题是使用同步操作()进行异步查找(
findById
),然后在异步操作完成之前保存文档。在尝试保存文档之前,您需要使用/、或某个异步库来确保完成所有异步操作
目前,代码流是:
- 查找模板(异步)
- 创建两个文档(同步)
- 映射到模板阵列(同步)
- 查找问题(异步)在保存之前,下面嵌套的所有内容都不会完成
- 创建新文档(同步)
- 映射到模板阵列(同步)
- 查找答案(异步)在保存之前,下面嵌套的所有内容都不会完成
- 尝试推送到阵列(同步)
- 尝试推送到阵列(同步)
- 保存文档(异步)
// Pseudo untested code focusing on the promise aspect only
// `create` is now a Promise
exports create = (body) => {
return qpTemplate.findOne().exec().then((template) => {
// Create projectModel and questionPackageModel documents
newProject.qpId = qp._id;
return Promise.all(
template.questionIds.map((theId) =>
questionTemplate.findById(theId).exec().then((question) => {
// Create questionModel document
qp.questionIds.push(theQuestion._id);
return Promise.all(
question.answerIds.map((answerId) =>
answerTemplate.findById(answerId).exec().then((answer) => {
// Create answerModel document
theQuestion.answerIds.push(answer._id);
return theAnswer.save();
)
).then(() => theQuestion.save());
}
).then(
() => Promise.all([qp.save(), newProject.save()])
).then(
() => {answerArray,questionArray,qp,newProject}
)
);
}
嘿,也许这篇关于Mongoose内置更新的文章可以帮助你:你的id函数在哪里?您是如何定义它的?您好,谢谢您的回答,但这里并没有涉及数组:PHi我这样定义它:var Id=mongoose.Types.ObjectId();你将如何编写它?我不明白,我一直在查找aync/await,但我似乎没有让它正常工作,是不是应该这样做。然后()?@Charlie我提供了一些伪代码来指导你,但如果你不熟悉承诺(对于承诺,异步/await是一种很好的糖类语法)然后我会开始研究承诺是如何工作的。谢谢:)我尝试了你的代码,我发现了这个错误,无法并行多次保存()同一个文档。我想我得多研究一下承诺。只是我只有这个功能,在我的整个项目中需要它。。