Tom Hanks
Tom Hanks

Reputation: 614

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

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

I know I have to wrap the callback in Meteor.bindEnvironment(), but I'm not sure how to do that with the promise? Any help would be greatly appreciated.

Upvotes: 0

Views: 188

Answers (1)

zim
zim

Reputation: 2386

i usually use futures for this. 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();
}

Upvotes: 2

Related Questions