猫鼬:将字符串推入数组
Mongoose: push a string into array
我有一个网站,任何登录用户都可以在该网站上留下对商店的评论。
所以基本上我有两个模式:
const journalSchema = new mongoose.Schema({
title: String,
category: String,
subcategory: String,
rating: Number,
review: [{type: String}],
link: String,
description: String,
});
const userSchema = new mongoose.Schema ({
username: String,
password: String,
journal: [{type: mongoose.Schema.Types.ObjectId, ref: 'Journal'}]
});
const Journal = mongoose.model("Journal", journalSchema);
const User = mongoose.model("User", userSchema);
来自 ejs 文件的表单:
<div class="container my-3">
<h1>Compose</h1>
<form class="" action="/stats" method="post">
<div class="form-group">
<label for="review">Description</label>
<textarea id="review" class="form-control" name="journalReview" rows="5" cols="30"></textarea>
</div>
<button class="btn btn-primary my-2" type="submit" name="button">Publish</button>
</form>
</div>
post 路线:
app.post("/stats", function(req, res){
if(req.isAuthenticated()){
const favJournal = req.body.savedJournal;
const userId = req.user.id;
const userReview = req.body.journalReview;
User.findById(userId, function(err, foundUser){
Journal.findById(favJournal, function(err, foundJournal){
if(err){
console.log(err);
}
else{
if(foundUser){
foundJournal.review.push(userReview);
foundJournal.save(function(){
console.log(foundJournal);
});
foundUser.journal.addToSet(foundJournal);
foundUser.save(function(){
res.redirect("/favourite");
});
}
}
});
})
.populate('journal')
.exec(function(err, user){
if(err) return handleError(err);
});
}
else{
res.redirect("/login");
}
});
每次我尝试从 ejs 文件推送评论时,我都会收到此错误:
events.js:353
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'review' of null
at C:\Users\HelloThere\Desktop\miesto\app.js:337:24
at C:\Users\HelloThere\Desktop\miesto\node_modules\mongoose\lib\model.js:5065:18
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event on Function instance at:
at C:\Users\HelloThere\Desktop\miesto\node_modules\mongoose\lib\model.js:5067:15
at processTicksAndRejections (internal/process/task_queues.js:77:11)
我尝试了类似 post 的不同解决方案。像 mongoose 方法:findOneAndUpdate 和 updateOne,但它们只是 return null.
您可以通过 findOneAnUpdate
和 $push
运算符来获取商店并使用 JavaScript 代码对其进行操作,然后将其保存回数据库。
例如,这个查询
Shop.findById( shopId, (shop) => {
shop.products.push(product);
shop.save();
}
可以通过这个查询来完成
Shop.findOneAndUpdate(
{ _id: shopId },
{ $push: {
products: product
}
})
$pop
、$push
和 $pull
是在 Mongoose 中操作数组的非常强大的工具。查看文档。
对于您收到的错误,我认为您收到错误是因为您向 findById 传递了错误的 journalId
。检查 MongoDB Compass 是否有具有该 ID 的文档 favJournal
我想我找到了问题的原因,我的 ejs 文件中有两个 post 表单,并且由于两个表单都有提交按钮,没有任何区别,所以只有第一个表单在post路线。
我有一个网站,任何登录用户都可以在该网站上留下对商店的评论。
所以基本上我有两个模式:
const journalSchema = new mongoose.Schema({
title: String,
category: String,
subcategory: String,
rating: Number,
review: [{type: String}],
link: String,
description: String,
});
const userSchema = new mongoose.Schema ({
username: String,
password: String,
journal: [{type: mongoose.Schema.Types.ObjectId, ref: 'Journal'}]
});
const Journal = mongoose.model("Journal", journalSchema);
const User = mongoose.model("User", userSchema);
来自 ejs 文件的表单:
<div class="container my-3">
<h1>Compose</h1>
<form class="" action="/stats" method="post">
<div class="form-group">
<label for="review">Description</label>
<textarea id="review" class="form-control" name="journalReview" rows="5" cols="30"></textarea>
</div>
<button class="btn btn-primary my-2" type="submit" name="button">Publish</button>
</form>
</div>
post 路线:
app.post("/stats", function(req, res){
if(req.isAuthenticated()){
const favJournal = req.body.savedJournal;
const userId = req.user.id;
const userReview = req.body.journalReview;
User.findById(userId, function(err, foundUser){
Journal.findById(favJournal, function(err, foundJournal){
if(err){
console.log(err);
}
else{
if(foundUser){
foundJournal.review.push(userReview);
foundJournal.save(function(){
console.log(foundJournal);
});
foundUser.journal.addToSet(foundJournal);
foundUser.save(function(){
res.redirect("/favourite");
});
}
}
});
})
.populate('journal')
.exec(function(err, user){
if(err) return handleError(err);
});
}
else{
res.redirect("/login");
}
});
每次我尝试从 ejs 文件推送评论时,我都会收到此错误:
events.js:353
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'review' of null
at C:\Users\HelloThere\Desktop\miesto\app.js:337:24
at C:\Users\HelloThere\Desktop\miesto\node_modules\mongoose\lib\model.js:5065:18
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event on Function instance at:
at C:\Users\HelloThere\Desktop\miesto\node_modules\mongoose\lib\model.js:5067:15
at processTicksAndRejections (internal/process/task_queues.js:77:11)
我尝试了类似 post 的不同解决方案。像 mongoose 方法:findOneAndUpdate 和 updateOne,但它们只是 return null.
您可以通过 findOneAnUpdate
和 $push
运算符来获取商店并使用 JavaScript 代码对其进行操作,然后将其保存回数据库。
例如,这个查询
Shop.findById( shopId, (shop) => {
shop.products.push(product);
shop.save();
}
可以通过这个查询来完成
Shop.findOneAndUpdate(
{ _id: shopId },
{ $push: {
products: product
}
})
$pop
、$push
和 $pull
是在 Mongoose 中操作数组的非常强大的工具。查看文档。
对于您收到的错误,我认为您收到错误是因为您向 findById 传递了错误的 journalId
。检查 MongoDB Compass 是否有具有该 ID 的文档 favJournal
我想我找到了问题的原因,我的 ejs 文件中有两个 post 表单,并且由于两个表单都有提交按钮,没有任何区别,所以只有第一个表单在post路线。