Hi guys,
I’m trying to retrive the id of an insert inside a Meteor Method inside a callback
Server:
Meteor.method({
longMethod: function (item) {
externalApiCall(item, function(err, result) {
return CollectionName.insert({result});
});
}
});
Client:
Meteor.call('longMethod', item, function(err, insertID) {
Flowrouter.go('dynamicPath', insertID);
});
this obviously doesn’t work, because it’s returning in the wrong place. How can retrieve the id of a successful insert nested in a callback?
Should I use a reactive variable? how?
You could consider wrapping the server-side external async call in Meteor.wrapAsync
.
Meteor.method({
longMethod: function (item) {
const wrappedExternalApiCall = Meteor.wrapAsync(externalApiCall);
try {
return wrappedExternalApiCall(item);
} catch (err) {
throw new Meteor.Error('oops', 'something bad happened');
}
}
});
1 Like
the situation actually is even worse:
Meteor.method({
longMethod: function (item) {
externalApiAuth(credentials, function(err, result) {
result.externalApiCall(item, function (err, result) {
return CollectionName.insert({result});
})});
});
}
});
can I wrapAsync multiple times?
p.s.: how do you color your code in discourse?
Well, what you really mean here is:
Meteor.methods({
longMethod: function(item) {
const wrappedExternalApiAuth = Meteor.wrapAsync(externalApiAuth);
const wrappedExternalApiCall = Meteor.wrapAsync(externalApiCall);
try {
wrappedExternalApiAuth(credentials);
const result = wrappedExternalApiCall(item);
return CollectionName.insert({ result });
} catch (err) {
throw new Meteor.Error('oops'), 'something bad happened'
}
}
});
P.S. use backticks:
```
code here
```
1 Like