I posted this in stackoverflow but I didn’t get any responses so I am posting this here.
0
I’m trying to update or insert data with upsert
, but it doesn’t work.
Neither update nor insert doesn’t work… but I don’t see any errors both in terminal and console.
Does anybody see something wrong in my code?
This is inside of function where I insert or update data, also I use SimpleSchema for the Measures collection.
questions.forEach(question => {
Measures.upsert(question._id, {
$set: {
title: question.text,
type:
question.type === QuestionType.SHORT_ANSWER
? MeasureType.TEXT
: MeasureType.MULTIPLE_CHOICE,
typeOptions: {frequency},
shortCodeOptions: {},
entity: System.getCurrentEntity(),
},
});
});
Reference : Meteor Upsert Restricted Collection 403 Error
Mongodb update and insert data at the same time
meteor version 2.5.0
Thank you!
ADDED
Update part is solved. The selector which in this case is {_id: question._id}
was not actually matched.
questions.forEach(question => {
const questionId = Measures.findOne({questionId: question._id}); // added
Measures.upsert(
{_id: questionId?._id},
{
$set: {
title: question.text,
type:
question.type === QuestionType.SHORT_ANSWER
? MeasureType.TEXT
: MeasureType.MULTIPLE_CHOICE,
typeOptions: {frequency},
shortCodeOptions: {},
entity: System.getCurrentEntity(),
},
},
);
});
But I still won’t be able to insert a new data, because if the date doesn’t exist, there is no questionId
.
I added question mark to {_id: questionId?._id}
just to hide a error TypeError: Cannot read properties of undefined (reading '_id') meteor
but this is not solving the issue.
How can I insert a new data?