簡體   English   中英

錯誤:流星代碼必須始終在光纖中運行-API響應

[英]Error: Meteor code must always run within a Fiber - API Response

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM