我正在尝试使用这个 NPM 包:Gumroad-API在我的 Meteor 应用程序中。当我尝试在 Promise 回调内部执行 Meteor 方法调用(或集合插入)时,我在服务器端遇到问题。
以下是我的两个 Meteor 方法的代码:
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()
时,回调总是停止(没有错误消息)里面。
当我替换 Meteor.call()
时,我得到了相同的行为与 Collection.insert()
如:Sales.insert({text:'test'});
.
最佳答案
老问题,但是,失败的原因是因为 Meteor 环境在您的回调中不可用。
警告这是未经测试的代码
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
关于javascript - 在 Promise 回调中调用 Meteor 方法 [停止且没有错误],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34377304/