在Promise Callback中调用Meteor方法[暂停没有错误] [英] Calling a Meteor Method inside a Promise Callback [Halting w/o Error]
问题描述
我正在尝试使用这个NPM软件包:在我的Meteor应用程序中 Gumroad-API 。当我尝试在Promise回调中进行Meteor方法调用(或集合插入)时,我在服务器端遇到问题。
I'm trying to use this NPM package: Gumroad-API inside my Meteor app. I'm encountering issues on the server side when I try to do a Meteor method call (or a collection insert) inside of the Promise callback.
以下是代码对于我的两个流星方法:
The following is the code for my two Meteor methods:
Meteor.methods({
testMethod: () => {
console.log('TEST METHOD RUN');
},
fetchGumroadData: () => {
const Gumroad = Meteor.npmRequire('gumroad-api');
let gumroad = new Gumroad({ token: Meteor.settings.gumroadAccessKey });
Meteor.call('testMethod'); // runs fine
gumroad.listSales('2014-12-04', '2099-12-04', 1).then((result) => {
console.log('1'); // runs fine
Meteor.call('testMethod'); // execution halts here w/o error msg
console.log('2'); // this never runs
});
},
});
.then()
内的代码每当我尝试在其中执行 Meteor.call()
时,回调总是停止(没有错误消息)。
The code inside the .then()
callback always halts (without error msgs) whenever I try to do a Meteor.call()
inside of it.
当我用 Collection.insert()
替换 Meteor.call()
时,我得到相同的行为,例如: Sales.insert({text:'test'});
。
I get the same behaviour when I replace the Meteor.call()
with a Collection.insert()
such as: Sales.insert({text:'test'});
.
推荐答案
老问题但是,失败的原因是因为Meteor环境在你的回调中不可用。
Old question but, the reason this fails is because the Meteor environment is not available in your callback.
警告这是未经测试的代码
Meteor.methods({
testMethod: () => {
console.log('TEST METHOD RUN');
},
fetchGumroadData: () => {
const Gumroad = Meteor.npmRequire('gumroad-api');
let gumroad = new Gumroad({ token: Meteor.settings.gumroadAccessKey });
Meteor.call('testMethod'); // runs fine
gumroad.listSales('2014-12-04', '2099-12-04', 1)
.then(Meteor.bindEnvironment((result) => {
console.log('1'); // runs fine
Meteor.call('testMethod'); // execution halts here w/o error msg
console.log('2'); // this never runs
}));
},
});
关于bindEnvironment和wrapAsync的教程可以在这里找到: https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment
Tutorials on bindEnvironment and wrapAsync can be found here: https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment
这篇关于在Promise Callback中调用Meteor方法[暂停没有错误]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!