there is an array cateogoryList in the defined Schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ProductCateogorySchema = new Schema({
user:{
type:Schema.Types.ObjectId,
ref:"users"
},
cateogoryList:[
{
cateogoryName:{
type:String,
required:true
},
cateogoryDes:{
type:String
}
},
{
cateogorySubList:[
]
}
],
date:{
type:Date,
default:Date.now
}
});
module.exports = mongoose.model("cateogories",ProductCateogorySchema);
now add data to this array through the form. Normally, you have to query the Schema first, find the cateogoryList, then add the data, and then save it. But I encounter this problem here:
cannot manipulate the cateogoryList array queried into the Schema, or maybe I don"t know how to operate it, and then I can"t write the data.
on the front is the method of this POST:
router.post("/cateogory/add",urlencodedParser,(req,res) => {
CateogoryOneSchema.find().then(cateogory => {
if(!cateogory){
console.log("cateogory :" + cateogory)
const newFields = {
cateogoryName: req.body.cateogoryOneName,
cateogoryDes:req.body.cateogoryOneDes
}
const category = {};
category.categoryList = [];
category.categoryList.push(newFields);
cateogory.save();
}
})
})
the cateogory printed to is empty data that has just been inserted unsuccessfully:
but when I use category.categoryList, I will report an error that I can"t use push. What should I do?
Thank you very much!