猫鼬 - 推送参考 - 无法读取未定义的 属性 "push"
Mongoose - pushing refs - cannot read property "push" of undefined
我想添加一个类别,如果成功,将它的引用推送到用户的集合。我就是这样做的:
那是我的 "dashboard.js" 文件,其中包含类别架构。
var users = require('./users');
var category = mongoose.model('categories', new mongoose.Schema({
_id: String,
name: String,
ownerId: { type: String, ref: 'users' }
}));
router.post('/settings/addCategory', function(req, res, next) {
console.log(req.body);
var category_toAdd = new category();
category_toAdd._id = mongoose.Types.ObjectId();
category_toAdd.name = req.body.categoryName;
category_toAdd.ownerId = req.body.ownerId;
category.findOne({
name: req.body.categoryName,
ownerId: req.body.ownerId
}, function(error, result) {
if(error) console.log(error);
else {
if(result === null) {
category_toAdd.save(function(error) {
if(error) console.log(error);
else {
console.log("Added category: " + category_toAdd);
<<<<<<<<<<<<<<<<<<<THE CONSOLE LOG WORKS GOOD
users.categories.push(category_toAdd);
}
});
}
}
});
这是我的 "users.js" 文件,其中包含 "users" 架构。
var categories = require('./dashboard');
var user = mongoose.model('users', new mongoose.Schema({
_id: String,
login: String,
password: String,
email: String,
categories: [{ type: String, ref: 'categories' }]
}));
所以,类别添加过程运行良好,我可以在数据库中找到类别。问题是当我试图将类别推送给用户时。
这一行:
users.categories.push(category_toAdd);
我收到这个错误:
Cannot read property "push" of undefined.
我需要再次承认,在推送之前 console.log 类别打印正确。
感谢您的宝贵时间。
users
对象是一个 Mongoose 模型,而不是它的一个实例。您需要 users
模型的正确实例才能将类别添加到。
dashboard.js
...
category_toAdd = {
_id: mongoose.Types.ObjectId(),
name: req.body.categoryName,
ownerId: req.body.ownerId
};
// Create the category here. `category` is the saved category.
category.create(category_toAdd, function (err, category) {
if (err) console.log(err);
// Find the `user` that owns the category.
users.findOne(category.ownerId, function (err, user) {
if (err) console.log(err);
// Add the category to the user's `categories` array.
user.categories.push(category);
});
});
我想添加一个类别,如果成功,将它的引用推送到用户的集合。我就是这样做的:
那是我的 "dashboard.js" 文件,其中包含类别架构。
var users = require('./users');
var category = mongoose.model('categories', new mongoose.Schema({
_id: String,
name: String,
ownerId: { type: String, ref: 'users' }
}));
router.post('/settings/addCategory', function(req, res, next) {
console.log(req.body);
var category_toAdd = new category();
category_toAdd._id = mongoose.Types.ObjectId();
category_toAdd.name = req.body.categoryName;
category_toAdd.ownerId = req.body.ownerId;
category.findOne({
name: req.body.categoryName,
ownerId: req.body.ownerId
}, function(error, result) {
if(error) console.log(error);
else {
if(result === null) {
category_toAdd.save(function(error) {
if(error) console.log(error);
else {
console.log("Added category: " + category_toAdd);
<<<<<<<<<<<<<<<<<<<THE CONSOLE LOG WORKS GOOD
users.categories.push(category_toAdd);
}
});
}
}
});
这是我的 "users.js" 文件,其中包含 "users" 架构。
var categories = require('./dashboard');
var user = mongoose.model('users', new mongoose.Schema({
_id: String,
login: String,
password: String,
email: String,
categories: [{ type: String, ref: 'categories' }]
}));
所以,类别添加过程运行良好,我可以在数据库中找到类别。问题是当我试图将类别推送给用户时。
这一行:
users.categories.push(category_toAdd);
我收到这个错误:
Cannot read property "push" of undefined.
我需要再次承认,在推送之前 console.log 类别打印正确。
感谢您的宝贵时间。
users
对象是一个 Mongoose 模型,而不是它的一个实例。您需要 users
模型的正确实例才能将类别添加到。
dashboard.js
...
category_toAdd = {
_id: mongoose.Types.ObjectId(),
name: req.body.categoryName,
ownerId: req.body.ownerId
};
// Create the category here. `category` is the saved category.
category.create(category_toAdd, function (err, category) {
if (err) console.log(err);
// Find the `user` that owns the category.
users.findOne(category.ownerId, function (err, user) {
if (err) console.log(err);
// Add the category to the user's `categories` array.
user.categories.push(category);
});
});