I have defined my filed and shema as:
storageArticles:
{
type: Array,
autoValue: function() {
return [];
},
label: 'Articless added to storage.',
},
'storageArticles.$': {
type: String
}
When I try to update this field by using (in my server side method):
Storages.update(storageId , {$set: { "storageArticles" : articleId }});
Everything goes OK, but data is no added to Array.
Can you give me some guidance to solve this.
EDIT
Edit adds more details on this question, maybe here is my mistake.
'articles.articleAddToStorage': function articleAddToStorage(storageId,articleId) {
check(storageId, String);
check(articleId, String);
try {
Articles.update(articleId, { $set: {articleAssignedToStorage:true}});
Storages.update(storageId , {$addToSet: { "storageArticles" : articleId }});
} catch (exception) {
handleMethodException(exception);
}
}
handleAddToStorage()
{
const articleId = this.props.articleId;
const storageId = this.state.storageId;
const history = this.props.history;
if(storageId!="")
{
Meteor.call('articles.articleAddToStorage', storageId,articleId, (error) => {
if (error) {
Bert.alert(error.reason, 'danger');
} else {
Bert.alert("Article assigned to storage", 'success');
}
});
}
else {
Bert.alert("Plese select storage", 'warning');
}
}
Using the field set operator
With the line
you basically try to set a string value (articleId) to a field that is defined as an array of strings.
This would only make sense if you set an array of values to
storageArticles(thus overriding the complete field):Using array update operators
If you want to push or pull values, you may look for the mongo array update operators (listing some examples here):