【问题标题】:Meteor: How to deal with async codeMeteor:如何处理异步代码
【发布时间】:2016-11-10 17:46:05
【问题描述】:

我正在尝试将发现的 BLE 设备的引用存储在 Meteor Mongo 集合中。

到目前为止,代码运行良好,但在Devices.insert({....}) 发生错误:

Meteor 代码必须始终在 Fiber 中运行。

我在 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

【问题讨论】:

  • 而不是noble.on('discover', function...)noble.on('discover, Meteor.bindEnvironment(function...)),这样这个函数就承载了Meteor的Fiber环境。如果这解决了您的问题,那么这个地方可能有一些重复。
  • 谢谢。它似乎有效 - 我将继续处理我的代码,稍后我会将问题标记为已解决。
  • 嗯,this question 差不多,但我对答案并不满意。我可能会去那里找一个规范的......
  • 感谢您的帮助 - 它工作正常。您可以发布您的第一条评论作为答案吗?

标签: javascript node.js meteor bluetooth-lowenergy


【解决方案1】:

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

代替noble.on('discover', function...) 做noble.on('discover, Meteor.bindEnvironment(function...)) 这样这个函数就承载了Meteor的Fiber环境。如果这解决了您的问题,那么这个地方可能有一些重复。

这里是关于这个主题的更多信息:https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2019-10-30
    • 2020-03-01
    • 2019-03-12
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2011-11-11
    相关资源
    最近更新 更多