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()中,但是我不确定如何用promise做到这一点?任何帮助将不胜感激。

最佳答案

我通常为此使用期货。例如

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

08-17 07:12