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);
uj5u.com熱心網友回復:
你可以這樣嘗試,假設我們為了簡單起見使用異步函式來避免 .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 “類別 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371147.html
標籤:节点.js 数组 MongoDB 猫鼬 mongodb-查询
下一篇:如何創建具有默認值的物件陣列?
