$push/$addToSet and returns error

I am creating my collection field as follow:

comments: {
        type: [String, String],
        label: 'CommentsLabel',
        optional: true,
},

I want the comments to have key, value pair that holds the user id as the key and his comment as the value,

When I try to enter value into the collection as follow:
Drawings.update({_id: id}, {$set: {comments: [Meteor.userId(), document.getElementById('commentTextArea').value]}})

The values are successfully stored in the collection:
Screen Shot 2020-06-28 at 9.48.00 AM

the problem is that the above code overrides the values, I want the user to push into the array as new comment with the new key, value pair, and I have tried both $push as well as $addToSet as follows:

Drawings.update({_id: id}, {$push: {comments:[Meteor.userId(), document.getElementById('commentTextArea').value]}});

Drawings.update({_id: id}, {$addToSet: {comments:[Meteor.userId(), document.getElementById('commentTextArea').value]}});

and both $push and $addToSet return the following error in the console:

update failed: Error: CommentsLabel must be a string (comments.0) in drawings update
    at getErrorObject (http://localhost:3000/packages/aldeed_collection2.js?hash=a69577047db366439087c686b90f269a3cd3a56a:579:17)
    at doValidate (http://localhost:3000/packages/aldeed_collection2.js?hash=a69577047db366439087c686b90f269a3cd3a56a:548:13)
    at Collection.Mongo.Collection.<computed> [as update] (http://localhost:3000/packages/aldeed_collection2.js?hash=a69577047db366439087c686b90f269a3cd3a56a:297:14)
    at Object.click #addComment (http://localhost:3000/app/app.js?hash=e62868eba963926f7b9df35445920518c27ffe15:2303:18)
    at http://localhost:3000/packages/blaze.js?hash=a20deb597f76789e171a9ee2b2e37d73fbb7ecda:3630:20
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=a20deb597f76789e171a9ee2b2e37d73fbb7ecda:3575:14)
    at Blaze.View.<anonymous> (http://localhost:3000/packages/blaze.js?hash=a20deb597f76789e171a9ee2b2e37d73fbb7ecda:3629:25)
    at http://localhost:3000/packages/blaze.js?hash=a20deb597f76789e171a9ee2b2e37d73fbb7ecda:2426:28
    at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?hash=a20deb597f76789e171a9ee2b2e37d73fbb7ecda:2109:12)
    at Blaze._DOMRange.<anonymous> (http://localhost:3000/packages/blaze.js?hash=a20deb597f76789e171a9ee2b2e37d73fbb7ecda:2425:24)

How to properly add key and value pair to the comments array?

UPDATE:
I tried the following:

var commentsLength = Drawings.findOne({_id: id}).comments.length;
				Drawings.update({_id: id}, {$set: {'comments.commentsLength':[Meteor.userId(), document.getElementById('commentTextArea').value]}});

But I get restriction related error in the console even though I am using $set operator :slight_smile:

update failed: Access denied. In a restricted collection you can only update documents, not replace them. Use a Mongo update operator, such as '$set'.

Not that even when I use $push operator, I get the same restriction error

below is my allow and deny rules for the collection:

Drawings.allow({
	insert: function(userId, doc){
		return !!userId;
	},
    update: function(userId, doc){
        return true;
    },
    remove: function(userId, doc){
        return doc.authorId === userId;
    }
});

What am I doing wrong here?

I’m guessing this is a simpl-schema/collection2 schema?
If so, you can’t declare types like that.
What you want is:

    comments: {
        type: Array,
        minCount: 2,
        maxCount: 2,
        label: 'CommentsLabel',
        optional: true,
    },
    'comments.$': {
         type: String
    }

This may or may not fix your issue, but it’s the first thing I see

2 Likes

Seems like this is accepted now

type: [ String ]

1 Like