Skip to content
Advertisement

meteor How to use upsert | Exception while simulating the effect of invoking ” TypeError: Cannot read properties of undefined (reading ‘_id’) react.js

I’m having trouble with upsert with meteor.js. I could update questions finely with the below codes but I won’t be able to insert new data.

file in client side

JavaScript

file in server side (collection file)

JavaScript

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 one as new data into the database. Is this wrong?

I switched $set to $setOnInsert, but still didn’t work…

ADDED

Now I don’t see any error but i couldn’t insert new data. I could update data tho.

JavaScript

Also removed const questionId = Measures..... part

Advertisement

Answer

You still need to set the questionId on insert:

JavaScript

Otherwise there will never be a doc that contains the questionId.

Note: Another helpful tool would be Collection2 and define a schema, so it throws errors, in case docs are inserted/updated that violate a schema. Using this would have thrown, that questionId is missing on insert.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement