我正在構建一個簡單的網絡應用程序,公司向其員工發送一個問題,請求反饋。仍在學習mongodb。一直玩弄它一週&我在論壇上慢慢獲得了一些有用的幫助,但只是現在我意識到我一直在使用有缺陷的思維過程來設計架構。我最初使用用戶的響應作爲UserSchema
一個領域,但現在我已刪除了它(如註釋掉這裏),因爲我意識到這不是用戶的財產而是不斷變化的變量(是/否/空) 。我現在必須創建一個單獨的AnswersSchema
(我被告知我需要一個,但我頑固地反對它 - 當我開始項目時沒有意識)我現在已經做了(糾正我,如果它被錯誤地寫/思考出)。我現在的問題是如何修改我的查詢在API的所有三個實體一起在save
操作在router
post
鏈接? 請注意save
此處顯示的操作代碼可行,但存在缺陷,因爲它適用於用戶將響應作爲其屬性之一。所以現在只有用戶的名字出現在角度前端,我刪除了UserSchema
的迴應,這是有道理的。快速:mongodb貓鼬鏈接實體
var QuestionSchema = Schema({
id : ObjectId,
title : String,
employees : [{ type: ObjectId, ref: 'User'}]
});
var UserSchema = Schema({
username : String,
//response : String,
questions : [{ type: ObjectId, ref: 'Question'}]
});
//new schema/collection I've had to create
var AnswerSchema = Schema({
response : {type :String, default:null},
question : { type: ObjectId, ref: 'Question'},
employees : [{ type: ObjectId, ref: 'User'}],
})
module.exports = mongoose.model('Question', QuestionSchema);
module.exports = mongoose.model('User', UserSchema);
module.exports = mongoose.model('Answer', AnswersSchema);
api.js
Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) {
//example data
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 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
}
@talwan再次感謝您的答覆。在'Answer.findOneAndUpdate({ 問題:req.body.question, 員工:req.body.user'事情是不會發生的角的HTTP沒有返回從API側的回答。當我做了'的console.log。 (req.body);'正上方'Answer.findOneAndUpdate'它顯示我的問題,名字和響應來自角發送的實際身體不知道以後 –
此外,雖然員工和響應實體得到保存到數據庫中會發生什麼。答案文件空 –
你是哪裏發出的迴應?難道數據庫條目顯示所有三個領域? –