reactjsmeteormeteor-react

meteor.js - upsert does not throw a console error, but doesn't insert data


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

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?


Solution

  • You are mixing things here and I am not sure if question is a document from the Measures collection or from a different collection.

    Option A - question is a document from the Measures collection

    If so, you need to get rid of questionId and use _id

            questions.forEach(question => {
              Measures.upsert(
                { _id: question._id },
                {
                  $set: {
                    title: question.text,
                    type:
                      question.type === QuestionType.SHORT_ANSWER
                        ? MeasureType.TEXT
                        : MeasureType.MULTIPLE_CHOICE,
                    typeOptions: {frequency},
                    shortCodeOptions: {},
                    entity: System.getCurrentEntity(),
                  },
                },
              );
            });
    

    Option B - question is a document from a different Collection

    Then you need to explicitly set questionId as field on $set:

            questions.forEach(question => {
              Measures.upsert(
                { questionId: question._id }},
                {
                  $set: {
                    title: question.text,
                    questionId: question._id, // required to associate measures with questions
                    type:
                      question.type === QuestionType.SHORT_ANSWER
                        ? MeasureType.TEXT
                        : MeasureType.MULTIPLE_CHOICE,
                    typeOptions: {frequency},
                    shortCodeOptions: {},
                    entity: System.getCurrentEntity(),
                  },
                },
              );
            });
    
    

    Tip: Context is important, which is why you should always clarify relations between documents and collections when asking questions.