Got error saying… Exception while simulating the effect of invoking 'modifyQuestion' TypeError: Cannot read properties of undefined (reading '_id')
I thought when {_id: questionId._id} got undefined, that’s the time upsert understand that there is no matching data found and insert as new data into the database.
Is this wrong?
it’s not the upsert error. The problem is the questionId is undefined.
you can change it from
{_id: questionId._id},
to
{_id: questionId?._id},
But it would lead you to other problem. The find condition will be empty object {}, which means it will match any record, then it will update the first one in your collection.
You should use 2 different cases for insert and update or modify the upsert condition like:
import { Random } from 'meteor/random'
...
{_id: questionId?._id || Random.id()},
it seems to me that this code is wrong… you don’t need to perform a findOne to do an upsert, you can simply just use it like this: {_id: question._id},
I’m sure that code doesn’t work. questionId is undefined.
When you’re not sure how to use upsert function, you can use insert and update instead.
For example:
if (document_exist) {
// call update function
} else {
// call insert function
}
while simulating the effect of invoking 'modifyQuestion' Error: When the validation object contains mongo operators, you must set the modifier option to true
I also tried removing findOne({}) and using if(question){ } else{ } but still got the same error…
Do you know what is meaning of this error? I’m googling but couldn’t figure out a answer