在 mongoose 中创建数据时无法读取未定义的 属性 'push'
Cannot read property 'push' of undefined when creating data in mongoose
我正在创建一个 express.js 应用程序,并且我在 mongoose 中创建了一个数据库用于测试目的。数据库在我导出到 app.js 文件的 function seedDB()
中。创建种子数据库没有错误,但是当我在该数据中添加一个新的 "review" 时。它说 cannot read property "push" of undefined
即使我的 mongoose 模型设置正确。
我在 mongoDB 中有两个集合,分别是 "tours" 和 "reviews"
我尝试使用 db.tours.find() 查找 mongo shell 中的游览,我发现我的 "review" 是一个与评论集合关联的数组,设置正确。但是当我查找 db.reviews.find() 时。它也在那里,但它的结果是我预期的结果。
我试过检查我是否只是忘记了括号、花括号,但我认为这不是问题所在。
我也试过一遍又一遍地查看我的模型并再次更改但也没有问题
const tours = require("./models/tours");
const Review = require('./models/reviews');
let tourData = [{
image: "image.jpg",
place: "Place",
name: "name",
description: "this is a description",
price: 1234,
info: "this is a great tour"},
{
image: "image.jpg",
place: "Place",
name: "name",
description: "this is a description",
price: 1234,
info: "this is a great tour"},
{
image: "image.jpg",
place: "Place",
name: "name",
description: "this is a description",
price: 1234,
info: "this is a great tour"},
]
function seedDB(){
tours.deleteMany({}, (err)=>{
if(err){
console.log(err);
}
console.log("removed tours!");
//add a few tours
tourData.forEach(function(seeds){
tours.create(seeds, (err, data)=> {
if(err){
console.log(err)
} else {
console.log('added all tours!');
//create a comment
Review.create(
{
text: "this place is great! ",
author: "Arnold"
}, (err, comment)=> {
if(err){
console.log(err)
} else {
tours.reviews.push(comment); //why is this undefined? I set it up correctly
tours.save();
console.log("created new review")
}
});
}
});
});
});
};
module.exports = seedDB
直到 console.log('added all tours!');
一切顺利,但是当我输入 Review.create()
时,它现在有错误,特别是 tours.reviews.push(comment);
//tours.js model
const mongoose = require('mongoose');
var ToursSchema = new mongoose.Schema({
image: String,
place: String,
name: String,
description: String,
price: Number,
info: String,
creator: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
reviews:[
{
type: mongoose.Schema.Types.ObjectId,
ref: "Review"
}
]
});
let Tours = mongoose.model('Tour', ToursSchema);
module.exports = Tours;
reviews.js 型号
const mongoose = require('mongoose');
var reviewSchema = mongoose.Schema({ //I also tried doing new Mongoose.Schema({
text: String,
author: String
});
module.exports = mongoose.model('Review', reviewSchema);
控制台中的预期结果应该是
removed tours!
added all tours!
added all tours!
added all tours!
created new review
created new review
created new review
mongo 数据库中的实际结果是 tours collections
中有一个 array of reviews
。
多项:
tours
是您的模型,而不是您的实例。您想要推送对您的一个或所有实例的评论,在您的情况下是 data
。因此,在您的情况下,您可以执行类似 data[0].reviews.push(comment)
的操作。我明白你是怎么把这个直觉搞混的,因为 tours
是小写字母,这让它看起来像一个实例,而不是模型。
在data
之后第二差的变量名是data2
:-P
考虑用更易于阅读和维护的 async/await 语法替换您的回调
不直接要求您的模型,而是注册您的模型并使用 mongoose.model('tours')
我正在创建一个 express.js 应用程序,并且我在 mongoose 中创建了一个数据库用于测试目的。数据库在我导出到 app.js 文件的 function seedDB()
中。创建种子数据库没有错误,但是当我在该数据中添加一个新的 "review" 时。它说 cannot read property "push" of undefined
即使我的 mongoose 模型设置正确。
我在 mongoDB 中有两个集合,分别是 "tours" 和 "reviews" 我尝试使用 db.tours.find() 查找 mongo shell 中的游览,我发现我的 "review" 是一个与评论集合关联的数组,设置正确。但是当我查找 db.reviews.find() 时。它也在那里,但它的结果是我预期的结果。
我试过检查我是否只是忘记了括号、花括号,但我认为这不是问题所在。 我也试过一遍又一遍地查看我的模型并再次更改但也没有问题
const tours = require("./models/tours");
const Review = require('./models/reviews');
let tourData = [{
image: "image.jpg",
place: "Place",
name: "name",
description: "this is a description",
price: 1234,
info: "this is a great tour"},
{
image: "image.jpg",
place: "Place",
name: "name",
description: "this is a description",
price: 1234,
info: "this is a great tour"},
{
image: "image.jpg",
place: "Place",
name: "name",
description: "this is a description",
price: 1234,
info: "this is a great tour"},
]
function seedDB(){
tours.deleteMany({}, (err)=>{
if(err){
console.log(err);
}
console.log("removed tours!");
//add a few tours
tourData.forEach(function(seeds){
tours.create(seeds, (err, data)=> {
if(err){
console.log(err)
} else {
console.log('added all tours!');
//create a comment
Review.create(
{
text: "this place is great! ",
author: "Arnold"
}, (err, comment)=> {
if(err){
console.log(err)
} else {
tours.reviews.push(comment); //why is this undefined? I set it up correctly
tours.save();
console.log("created new review")
}
});
}
});
});
});
};
module.exports = seedDB
直到 console.log('added all tours!');
一切顺利,但是当我输入 Review.create()
时,它现在有错误,特别是 tours.reviews.push(comment);
//tours.js model
const mongoose = require('mongoose');
var ToursSchema = new mongoose.Schema({
image: String,
place: String,
name: String,
description: String,
price: Number,
info: String,
creator: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
reviews:[
{
type: mongoose.Schema.Types.ObjectId,
ref: "Review"
}
]
});
let Tours = mongoose.model('Tour', ToursSchema);
module.exports = Tours;
reviews.js 型号
const mongoose = require('mongoose');
var reviewSchema = mongoose.Schema({ //I also tried doing new Mongoose.Schema({
text: String,
author: String
});
module.exports = mongoose.model('Review', reviewSchema);
控制台中的预期结果应该是
removed tours!
added all tours!
added all tours!
added all tours!
created new review
created new review
created new review
mongo 数据库中的实际结果是 tours collections
中有一个 array of reviews
。
多项:
tours
是您的模型,而不是您的实例。您想要推送对您的一个或所有实例的评论,在您的情况下是data
。因此,在您的情况下,您可以执行类似data[0].reviews.push(comment)
的操作。我明白你是怎么把这个直觉搞混的,因为tours
是小写字母,这让它看起来像一个实例,而不是模型。在
data
之后第二差的变量名是data2
:-P考虑用更易于阅读和维护的 async/await 语法替换您的回调
不直接要求您的模型,而是注册您的模型并使用 mongoose.model('tours')