使用 monogoose 将对象推送到节点 js 中的数组属性时出现错误
Posted
技术标签:
【中文标题】使用 monogoose 将对象推送到节点 js 中的数组属性时出现错误【英文标题】:I am getting an error while i am pushing an object to the array property in node js using monogoose 【发布时间】:2022-01-07 05:49:40 【问题描述】:My Problem is i want after i create the categoryName and then i create the product properties, then i can push the product properties to the categoryProduct field
.
我尝试使用 $push 并在数据库中给了我一个空数组。
CallBack Function for creating a product
//Here i am getting the values from the body
//create an object
const productObject = new productSchema(
productName: req.body.productName,
productPrice: req.body.productPrice,
productCategory: req.body.productCategory,
productQuantity: req.body.productQuantity,
productSection: req.body.productSection,
productExDate: req.body.productExDate
)
//saving
productObject
.save()
.then(data =>
res.redirect('/halalMunchies/all-products');
)
.catch(err =>
res.status(500).send(
message: err.message || "Some error occured while creating a create operation"
);
);
//pushing inside the productCategory in the category model
categoryDB.findOneAndUpdate( categoryName: req.body.productCategory , $push: productsCategory: productObject._id )
.then(result =>
console.log(result);
)
.catch(err =>
console.log(err);
)
the output
_id: new ObjectId("61a62e619c17c622153c4d1a"),
categoryName: 'meat',
productsCategory: [],
__v: 0
在categoryschema
中我有categoryname
和productsCategory
包含该类别拥有的所有产品。
Category Schema
var categorySchema = new mongoose.Schema(
//properties // shape of the documentation
categoryName:
type: String,
required: true,
unique: true
,
productsCategory: [
type: mongoose.Schema.Types.ObjectId,
ref: 'productSchema',
required: true
]
);
const categoryDB = mongoose.model('categorySchema', categorySchema);
在productSchema
中,它的一个属性是productCategory
,它引用categorySchema
var productSchema = new mongoose.Schema(
//defining the properties
productName:
type: String,
unique: true,
required: [true, 'Product name is required'] // we can pass a message like this
,
productCategory:
type: mongoose.Schema.Types.String,
ref: 'categorySchema',
required: [true, 'Category name is required'] // we can pass a message like this
,
productPrice:
type: Float,
required: [true, 'Price name is required'] // we can pass a message like this
,
productQuantity:
type: Number,
required: [true, 'Quantity name is required'] // we can pass a message like this
,
productSection:
type: String,
required: [true, 'Section name is required'] // we can pass a message like this
,
productExDate:
type: String,
required: [true, 'ExDate name is required'] // we can pass a message like this
)
const productDB = mongoose.model('productSchema', productSchema);
【问题讨论】:
【参考方案1】:你可以这样尝试,假设我们为了简单起见使用异步函数来避免 .then .catch 痛苦的过程:
const
productData,
productCategory,
= req.body;
const productObject = new productSchema( ...productData );
await productObject.save();
const categoryObject = await categorySchema.findOne( categoryName: productCategory );
if (!categoryObject)
// Throw some error
await categoryObject.productsCategory.push(productObject._id);
await categoryObject.save();
// then make your redirect to /halalMunchies/all-products
编辑
const
productName,
productPrice,
productQuantity,
productSection,
productExDate,
productCategory,
= req.body;
const productObject = new productSchema(
productName,
productPrice,
productCategory,
productQuantity,
productSection,
productExDate,
);
await productObject.save();
如果您的意思是 productCategory "category id",那么您应该通过 _id 获取:
const categoryObject = await categorySchema.findOne( _id: productCategory );
if (!categoryObject)
// Throw some error
await categoryObject.productsCategory.push(productObject._id);
await categoryObject.save();
// then make your redirect to /halalMunchies/all-products
【讨论】:
categorySchema.findOne( categoryName: productCategory );
这行应该只返回用户输入的categoryName,并且你设置它等于categoryObject,所以这个对象只有一个属性。在这个await categoryObject.productsCategory.push(productObject._id);
productsCategory 中未定义。
根据您在上面定义的 categorySchema,productsCategory 不能是未定义的,我提供的代码所做的就是它获取 categoryName 等于 productCategory 的类别,如果有这样的文档,它将通过推送新创建的 productObject 的 id 来更新 productsCategory 属性字段。如果 productsCategory 未定义,正如您所说的那样,首先创建类别文档时出现问题。
你能再看一下吗,我更新了创建对象以获取正文值的方式,我该如何基于此进行。谢谢你的帮助
感谢人的帮助以上是关于使用 monogoose 将对象推送到节点 js 中的数组属性时出现错误的主要内容,如果未能解决你的问题,请参考以下文章