首页 文章

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

提问于
浏览
0

main.js - Server

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 回答

  • 2

    我通常会使用期货 . 例如

    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();
    }
    

相关问题