How do you get the last inserted id in a Meteor.call back to the event?
In a Session or reactive var.
Meteor.call('aMethod', someArg, function(err, res){
//blah
Session.set('fromMethod', res);
})
Hmm. Returns undefined.
Meteor.call("addImage",orgname,name,size,source, function (error, result) {
Session.set("imageId",result)
console.log(result);
})
I would check the method.
It creates the document.
addImage: function(orgname,name,size,source){
if (!Meteor.userId()) {
this.stop()
}
Images.insert({
orgname: orgname,
name: name,
size: size,
source: source,
userId: Meteor.userId(),
createdAt: new Date()
});
}
you need to return
something back to the .call()
if you’re OK with a blocking insert then
addImage: function(orgname,name,size,source){
if (!Meteor.userId()) {
this.stop()
}
return Images.insert({
orgname: orgname,
name: name,
size: size,
source: source,
userId: Meteor.userId(),
createdAt: new Date()
});
}
else Insert has a callback that gives you the _id as the 2nd argument
you’ll need to return that, but use some async magic
See this post and the rest of the thread:
Got it. Thank you for your help. I was on that path before but added it to var and then returned that, and that didn’t work. Now it does.
UPDATE: I understand now. When using the var before I didn’t have the callback in the call.
on server:
addImage: function(orgname, name, size, source) {
if (!Meteor.userId()) {
this.stop()
}
var id = Images.insert({
orgname: orgname,
name: name,
size: size,
source: source,
userId: Meteor.userId(),
createdAt: new Date()
});
return id;
}
on client:
Meteor.call('addImage', name, size, source, function(err, res) {
if (err) {
console.log(err);
throw new Meteor.Error('It did not work!')
} else {
this.result = new ReactiveVar(res);
}
});
var id = Template.instance().result.get();
console.log(id);
Almost there.
How do I wait/paus for a Session to be set, have a value?
Thanks. Maybe better with new ReactiveVar(res). Is that faster, more reliable than Session?
Do I need to install a package for using that? Get undefined now.
ReactiveVar (atmosphere).
Main difference to a Session is, that it is not global.
Maybe do it like this:
Template.yourTemplate.onCreated(function() {
this.result = new ReactiveVar();
});
if the end data is going into a template then you don’t need to do anything other than return the reactive source via helper in to the template
if you want to use the end data in code then you need to put the reactive source into a tracker so that you can do something when it changes
I was trying to update an image file before upload to S3 adding the id from the document as a unique name, having created the document before upload. Somehow it didn’t always work as if the script didn’t had time to set the Session. Maybe because my slow pc. I skipped this and set the file name first before insert by adding Date.now() and a random string to the file name. Should be unique enough.
Thank you for all help.
I also like using the collection hooks package for after insert actions and the like. Especially if it’s for doing modifications that the client doesn’t need to handle/initiate.
Not sure if this is related to what you are doing but thought I might mention it.