错误:Meteor代码必须始终在Fiber-API响应中运行

时间:2017-02-02 02:45:49

标签: javascript meteor

main.js - 服务器

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { CurrentWeather } from '../collections/currentWeatherCollection.js';
import ForecastIo from 'forecastio';

if (Meteor.isServer) {
    var forecastIo = new ForecastIo('c997a6d0090fb4f6ee44cece98e9cfcc');
    forecastIo.forecast('30.533', '-87.213').then(function(data) {
      const currentWeather = JSON.stringify(data.currently, null, 2);
      CurrentWeather.insert({currentWeather: currentWeather});
    });
}

我知道我必须在Meteor.bindEnvironment()中包装回调,但我不知道如何用这个承诺做到这一点?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

我通常会使用期货。 e.g。

let Future = Npm.require('fibers/future');

if (Meteor.isServer) {
    let future = new Future();

    var forecastIo = new ForecastIo('c997a6d0090fb4f6ee44cece98e9cfcc');
    forecastIo.forecast('30.533', '-87.213').then(function(data) {
      const currentWeather = JSON.stringify(data.currently, null, 2);
      CurrentWeather.insert({currentWeather: currentWeather});

      future.return(currentWeather);
    });

    return future.wait();
}