0
フィードバックを求める従業員に質問を送信するシンプルなアプリケーションを構築しています。私は質問文書の中にもっと多くの従業員(ユーザー)を配列として配置しようとしています。今は1人の従業員しか挿入しません。基本的に私が必要とするのは、従業員が回答したすべての質問に対して(将来)dbにクエリを出すことができるように、各質問に従業員に回答させることです(&)。ここに私のスキーマです。Express:配列へのPOSTデータmongoose&node
ここにはprevious issue that was solvedがあります。興味のある方はこちらをご覧ください。私はあなたを修正します
モデル
var QuestionSchema = Schema({
id : ObjectId,
title : String,
employees : [{ type: ObjectId, ref: 'User'}]
});
module.exports = mongoose.model('Question', QuestionSchema);
var UserSchema = Schema({
username : String,
response : String,
questions : [{ type: ObjectId, ref: 'Question'}]
});
module.exports = mongoose.model('User', UserSchema);
api.js
Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) {
//example: is this the right way of creating the array
var user = new User([{
"username": "lindelof",
"response": "yes",
},{
"username": "bailly",
"response": "no",
},{
"username": "suzan",
"response": "yes",
}]);
question.employees = [user1._id];
user.questions = [question._id];
question.save(function(err) {
if (err) throw err;
console.log(question);
user1.save(function(err) {
if (err) throw err;
});
});
});
console.log('entry saved to answer >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
}
働いたおかげで! 'async'メソッドは' async function doJob() 'を指しても" SyntaxError:Unexpected token function "エラーをスローします。 –
あなたは古いnodejsバージョンを持っているでしょう。 – Lazyexpert