Simpl-Schema - Update Array Problem


#1

Hey guys,

I have a schema that contains an array of objects and am trying to $push an item to it. Quite simple: A survey with questions and answers. However, I keep getting the same error:

Exception while simulating the effect of invoking 'addQuestion' Error: After filtering out keys not in the schema, your modifier is now empty

I found this discussion https://stackoverflow.com/questions/44511368/cant-update-array-of-objects but the proposed solution didn’t work for me.

Here is my code:

imports/api/collections.js

export const Surveys = new Mongo.Collection('surveys');

const SurveySchema = new SimpleSchema({
    name: {
        type: String,
        label: "Name",
        optional: true
    },
    companyId: {
        type: String,
        label: "CompanyId",
        optional: true
    },
    questions: {
        type: Array,
        label: "Questions",
        optional: true
    },
    'questions.$': {
        type: Object,
        label: "Question",
        optional: true
    },
    'questions.$.type': {
        type: String,
        label: "QuestionType",
        optional: true
    },
    'questions.$.text': {
        type: String,
        label: "QuestionText",
        optional: true
    },
    'questions.$.responses': {
        type: Array,
        label: "Responses",
        optional: true
    },
    'questions.$.responses.$': {
        type: Object,
        label: "Response",
        optional: true
    },
    'questions.$.responses.$.respondent': {
        type: String,
        label: "RespondentId",
        optional: true
    },
    'questions.$.responses.$.answer': {
        type: String,
        label: "RespondentAnswer",
        optional: true
    }
});

Surveys.attachSchema(SurveySchema);

imports/api/methods.js

Meteor.methods({

    'addSurvey': function(name,companyId){

        Surveys.insert({
            name: name,
            companyId: companyId,
            questions: []
        });
    
    },

    'addQuestion': function(surveyName,questionType,questionText){

        Surveys.update({name:surveyName},{$push:{'questions.$':{'questions.$.type':questionType,'questions.$.text':questionText,'questions.$.responses':[]}}});

    }

});

imports/ui/question.js // this is a helper js file to the question.html page

if(Meteor.isClient){

    let companyId = Meteor.userId();

    Meteor.subscribe('surveys',companyId);

    Template.Question.events({

        'submit #newQuestion': function(event){
            
            event.preventDefault();
            
            const surveyName = event.target.chooseSurveyName.value;

            const questionType = 'Test';

            const questionText = event.target.questionText.value;

            Meteor.call('addQuestion',surveyName,questionType,questionText);
            
        }

     });
}

Any idea what I am doing wrong?


#2

That doesn’t look like how you’d push something to an array:

Surveys.update({name:surveyName},{$push:{questions: {type: questionType, text: questionText, responses: []}}})