Reputation: 1367
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');
}
}
Upvotes: 0
Views: 147
Reputation: 8413
With the line
Storages.update(storageId , {$set: { "storageArticles" : articleId }});
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):
Storages.update(storageId , {$set: { "storageArticles" : [articleId] }});
If you want to push or pull values, you may look for the mongo array update operators (listing some examples here):
$addToSet Adds elements to an array only if they do not already exist in the set.
Storages.update(storageId , {$addToSet: { "storageArticles" : articleId }});
$pop Removes the first or last item of an array.
Storages.update(storageId , { $pop : 1 });
$pull Removes all array elements that match a specified query.
Storages.update(storageId , {$pull: { "storageArticles" : articleId }});
$push Adds an item to an array.
Storages.update(storageId , {$push: { "storageArticles" : articleId }});
$pullAll Removes all matching values from an array.
Storages.update(storageId , {$pullAll: { "storageArticles" : [articleId1, articleId2] }});
Upvotes: 3