Meteor:如何处理异步代码

时间:2016-07-08 08:10:03

标签: javascript node.js meteor bluetooth-lowenergy

我正在尝试将已发现的BLE-Devices的引用存储在Meteor Mongo Collection中。

到目前为止,代码工作正常,但在Devices.insert({....})发生错误:

  

流星代码必须始终在光纤内运行。

我在Ubuntu 16.04 LTS上使用Meteor 1.3.4.1。

import noble from 'noble';

//Meteor mongo collection
import {Devices} from '../api/devices';

//cleanup collection before start
Devices.remove({});

//store found physical devices and all data
var PhyDevices = [];

noble.on('stateChange', function (state) {

    if(state == 'poweredOn') {

        console.log('scanning...');
        noble.startScanning([], true);
    } else {
        noble.stopScanning;
    }
})


noble.on('discover', function (peripheral){
    addToKnownDevices(peripheral);
});

function addToKnownDevices (peripheral) {
    if(PhyDevices.indexOf(peripheral) == -1){
        PhyDevices.push(peripheral);
        var deviceIndex = PhyDevices.indexOf(peripheral);

        //here is error --> "Meteor code must always run within a Fiber. "
        Devices.insert({
            name: peripheral.advertisement.localName,
            index: deviceIndex
        });

        console.log("Pushed " + peripheral.advertisement.localName + " with index " + deviceIndex);

    }
}

=> Meteor server restarted
I20160708-09:53:18.191(2)? scanning...
W20160708-09:53:18.707(2)? (STDERR) 
W20160708-09:53:18.709(2)? (STDERR) /home/cleitgeb/WebstormProjects/BLEScanner/.meteor/local/build/programs/server/packages/meteor.js:1060
W20160708-09:53:18.709(2)? (STDERR)     throw new Error("Meteor code must always run within a Fiber. " +
W20160708-09:53:18.710(2)? (STDERR)           ^
W20160708-09:53:18.741(2)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20160708-09:53:18.742(2)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20160708-09:53:18.742(2)? (STDERR)     at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:85:1)
W20160708-09:53:18.742(2)? (STDERR)     at addToKnownDevices (imports/scanner.js:42:34)
W20160708-09:53:18.743(2)? (STDERR)     at Noble. (imports/scanner.js:29:5)
W20160708-09:53:18.743(2)? (STDERR)     at Noble.emit (events.js:95:17)
W20160708-09:53:18.743(2)? (STDERR)     at Noble.onDiscover (/home/cleitgeb/WebstormProjects/BLEScanner/node_modules/noble/lib/noble.js:135:10)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].emit (events.js:106:17)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].NobleBindings.onDiscover (/home/cleitgeb/WebstormProjects/BLEScanner/node_modules/noble/lib/hci-socket/bindings.js:169:10)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].emit (events.js:106:17)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].Gap.onHciLeAdvertisingReport (/home/cleitgeb/WebstormProjects/BLEScanner/node_modules/noble/lib/hci-socket/gap.js:193:10)
=> Exited with code: 8

1 个答案:

答案 0 :(得分:0)

Kyll的评论里面有解决方案:

  

而不是贵族。('发现',功能...)做贵重。('发现,Meteor.bindEnvironment(功能......)),以便此功能携带流星的光纤环境。如果这解决了你的问题,那么这个地方可能会有一些重复。

以下是有关此主题的信息:https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment