【发布时间】:2018-01-04 00:01:42
【问题描述】:
我正在尝试通过
app.post("/api/post/:chorecomplete", function(req, res){
Parent.findOneAndUpdate({parentFirstName: req.body.parentFirstName, parentLastName: req.body.parentLastName, "chores.choreName": "firstChore"}, {$set: {"chores.$.complete": "true"}}).exec(function(err, doc){
if (err){ console.log(err); res.send("not ok");}
else{
console.log(doc);`enter code here`
res.send(ok);
}
})
})
}
我的服务器记录了猫鼬的响应,但不是因为我也在告诉它。我注释掉了 console.log(doc) 并对其进行了测试以查看我的数据库是否正在返回它或者我的服务器是否正在记录它。事情是,我的服务器以“Ok”响应,这意味着猫鼬没有抛出错误,但它没有修改我的
POST /api/post/chorecomplete 200 17.378 ms - 2
[1] { _id: 597aa48313cf6914905916e0,
[1] parentFirstName: 'firstName',
[1] parentLastName: 'lastName',
[1] parentEmail: 'mich@ael.com',
[1] __v: 0,
[1] children: [],
[1] chores:
[1] [ { choreName: 'firstChore',
[1] choreDesc: 'firstDescription',
[1] value: null,
[1] complete: false },
[1] { choreName: 'secondChore',
[1] choreDesc: 'secondDescription',
[1] choreValue: null,
[1] complete: false }] }
我不知道我做错了什么。我在那里有一个静态查询参数,但“firstChore”最终将是 req.body.choreName。我开始怀疑我是否应该为家务制作一个集合并将其引用到我的父模式和子模式,但我不是 100% 确定如何去实现对父和子的引用,这将相互引用.. . (Parent 和 Child 都有单独的用户帐户,Child 将更新要完成的家务活,并将收到 choreValue [在本例中应该是 $5.00,但由于某种原因 mongoose 不接受它。目前不是除了表单值之外的任何地方定义。])。这是一个示例模式。 (我试图尽可能详细。已经很晚了,这是我在这里的第一个问题。)
```//父模式 var 猫鼬 = 要求(“猫鼬”); var Schema = mongoose.Schema;
var ParentSchema = new Schema({ 父母名字:{ 类型:字符串, 要求:真 },
parentLastName: {
type: String,
required: true
},
parentEmail: {
type: String,
required: true,
validate: [
function(input){
input.length >= 3;
},
"Email must be a valid email"
]
},
password: {
type: String,
//required: true,
validate: [
function(input){
input.length >= 6;
},
"Password must be at least 6 characters"
]
},
chores: {
type: Array,
},
children: [{
type: Schema.Types.ObjectId,
ref: "Child"
}]
});
var Parent = mongoose.model("Parent", ParentSchema);
module.exports = 父级;
```
这是向数组添加杂项的路径。它将是表单值。在这条路线中有效地定义了一项杂务,而不是明确定义为它自己的模型。
//route for inserting chores
app.post("/api/post/:chores", function(req, res){
//When we have someone logged in we will take one of the values we get from their presence, (either _id or email) and replace my name. It's only my name b/c it was the name I initially inserted into the db.
//if chores === chores then findAll else if {var theChoreToFind === req.params.chores} and we'll run that chore to update a chore?
var parentFirstName = req.body.parentFirstName;
var parentLastName = req.body.parentLastName;
var choreName = req.body.choreName;
var choreDesc = req.body.choreDesc;
var choreValue = req.body.choreValue;
Parent.findOneAndUpdate({parentFirstName: req.body.parentFirstName, parentLastName: req.body.parentLastName}, {$push: {chores: {choreName: choreName, choreDesc: choreDesc, choreValue: choreValue, complete: false}}}).exec(function(err, doc){
if(err) {console.log(err)}
console.log(doc);
})
res.send("Ok");
})//END new Chores
这是令人难以置信的长篇大论。我只是不想让我提出的任何东西不在这里供参考。如果你已经做到了这一步。谢谢。
【问题讨论】: