I'm making a MeteorJS app, where I fetch multiple files from remote API.
One API record can have multiple files - I want to download them, save in my Media
collection and retrieve their id to store into another collection (e.g. NotMedia
):
// Client side: apiArr is an array with api json objects
for(var a in apiArr){
var pic1_id = Meteor.call('saveMedia', apiArr[a].image1_url);
var pic2_id = Meteor.call('saveMedia', apiArr[a].image2_url);
var collection = {
name: apiArr[a].name,
description: apiArr[a].desc,
image1_id: pic1_id,
image2_id: pic2_id
};
NotMedia.insert(collection);
}
my Meteor method:
//Server side:
Meteor.methods({
saveMedia: function(media){
var file = new FS.File();
file.attachData(media, function(err){
if(err){
throw err;
}
file.origin_name = media;
var id = Media.insert(file);
return id;
});
},
});
But when I retrieve saved NotMedia
record, image_id
's are undefined
. I understand that NotMedia
insertion is finished faster than saving image files, but how can I synchronize that/wait for Meteor.calls
to finish?