Meteor, SimpleSchema, meteor-collection2 - Adding to array


Meteor, SimpleSchema, meteor-collection2 - Adding to array



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');

}
}




1 Answer
1



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):


storageArticles


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] }});





By using Storages.update(storageId , {$addToSet: { "storageArticles" : articleId }}); I get this error: update failed: Error: Storage articles must be of type String
– Sysrq147
Jul 2 at 20:29






On a new or an existing doc? I ask because when you try it on the doc where you previously set the field to an articleId, which is a string, the field is not an array anymore. Using an array update method on it will then likely result in that error.
– Jankapunkt
Jul 2 at 20:45





I have open Robo 3T and I see that type of storageArticles is Array, but when I try to update it it does not work, I have edited my question with more details.
– Sysrq147
Jul 2 at 20:51





Try removing the autoValue - you don't need to initialize an empty array.
– Michel Floyd
Jul 2 at 21:15



autoValue





Thank You @MichelFloyd, this together with Mr. Jankapunkt suggestion solved the issue.
– Sysrq147
Jul 3 at 15:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages