在meteor中使用bluebird库将远程API中的数据保存到本地mongo db中

时间:2016-11-04 06:48:24

标签: mongodb meteor promise bluebird

Meteor.methods({
'sync.toggl'(apiToken) {

  const toggl = new TogglApi({ apiToken });

  Promise.promisifyAll(toggl);

  toggl.getWorkspacesAsync()
    .each(ws => toggl.getWorkspaceProjectsAsync(ws.id)
      .map(p => {
        Projects.upsert({ projectId: p.id }, {
          projectId: p.id,
          name: p.name,
          tracker: 'toggl',
          tags: [],
          contributors: []
        });
      })
      .catch(err => console.error(`fetching ${ws.name} projects error - ${err.message}`));
  )
  .catch(err => console.error(`fetching ${ws.name} workspace error - ${err.message}`));
}});

我正在尝试将toggl api中的数据保存到本地数据库中。但Meteor抛出一个错误 - Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.我找到了几个解决方案,但他们不允许我使用蓝鸟承诺......或者不是?

1 个答案:

答案 0 :(得分:0)

使用<context:property-placeholder location="config.properties" /> 为我工作:

async/await