'How do I push my new item without creating new mongoose item
I want to append or push my new items that I input in the array without creating new mongoose schema..what I mean is that I will only push items without creating new _id...Here is my code..
For mongoose.Schema()
const mainSchema = new Schema({
likes:{
type:Number,
max:100000
},
people:[{
key:{type:String},
name:{type:String}
}]
},{
timestamps:true
})
And for the routes that will post my items..
router.route('/item/1').post((req,res) => {
const { likes, people } = req.body
const FirstItem = Main({
likes,
// people
})
FirstItem.people.push(people)
FirstItem.save()
.then(likes => res.json('New User Added'))
.catch(err => res.status(400).json('Error :' + err))
})
As you see I didn't input new Main({}) in my post because I don't wanna create a new _id..but I want to push my items in my array whenever I create another new items....This is how I write it in postman..
{
"likes":0,
"people":[
{
"key":"Tes22t",
"name":"Tit213an"
}
]
}
Now if I changed something after posting this in my POST method. like "key":"testetsee","name":"123123123" it will give me an error like this ...
"Error :ValidationError: people.0._id: Cast to ObjectId failed for value \"[ { key: 'Tes22t', name: 'Tit213an' } ]\" (type Array) at path \"_id\""
I think my problem here is pushing the item? or the post? or do I just need to update it?
EDITED
I have this in my frontend to pass it to my backend because I want my each item to received whose account has registered so that it can store the username and keys..
axios.post('http://localhost:7171/likes/item/'+1,{ people:{name:name,key:key }})
axios.post('http://localhost:7171/likes/item/'+2,{ people:{name:name,key:key } })
axios.post('http://localhost:7171/likes/item/'+3,{ people:{name:name,key:key } })
axios.post('http://localhost:7171/likes/item/'+4,{ people:{name:name,key:key } })
axios.post('http://localhost:7171/likes/item/'+5,{ people:{name:name,key:key } })
And then the code you give me sir is here ...
router.route('/item/:id').post((req,res) => {
const id = req.params.id
console.log(id)
if (!id) return res.status(400).json({ message: "missing id" });
const { likes, people } = req.body;
Main
.updateOne(
{ _id: id },
{
$addToSet: { people: { $each: people } },
$set: {
likes,
},
},
)
.then((likes) => res.json({ message: "New User Added" }))
.catch((err) => res.status(400).json("Error :" + err));
})
Since there is 5 items that I will post simultaneously, then It should have 5 items in my database and will update the array whenever I have new user to append it in my list of array in each item object.
What I received in my postman is this...
"Error :CastError: Cast to ObjectId failed for value \"1\" (type string) at path \"_id\" for model \"liker-model\""
Solution 1:[1]
If you only want push an element into people array of that object, you should use updateOne() instead of save()
So the code will like
router.route('/item/1').post((req,res) => {
const { likes, people } = req.body
const model = mongoose.model('collection_name', mainSchema);
model.updateOne({like}, {$addToSet: { people: {$each: people} }})
.then(likes => res.json('New User Added'))
.catch(err => res.status(400).json('Error :' + err))
})
after discuss with the thread owner, i create new answer with the /1 as item id
const _ = require("lodash");
router.route("/item/:id").post((req, res) => {
const id = _.get(req, "params.id");
if (!id) return res.status(400).json({ message: "missing id" });
const { likes, people } = req.body;
const model = mongoose.model("collection_name", mainSchema);
model
.updateOne(
{ _id: id },
{
$addToSet: { people: { $each: people } },
$set: {
likes,
},
},
)
.then((likes) => res.json({ message: "New User Added" }))
.catch((err) => res.status(400).json("Error :" + err));
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
