Meteor:如何处理异步代码
Meteor: How to deal with async code
我正在尝试将已发现的 BLE 设备的引用存储在 Meteor Mongo 集合中。
到目前为止代码工作正常,但在 Devices.insert({....})
处发生错误:
Meteor code must always run within a 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) <b>Error: Meteor code must always run within a Fiber.</b> 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
</pre>
Kyll 的评论中有解决方案:
Instead of noble.on('discover', function...) do noble.on('discover, Meteor.bindEnvironment(function...)) so that this function carries the Fiber environment of Meteor. If this solved your issue, then there's probably some duplicates around the place.
以下是有关此主题的更多信息:https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment
我正在尝试将已发现的 BLE 设备的引用存储在 Meteor Mongo 集合中。
到目前为止代码工作正常,但在 Devices.insert({....})
处发生错误:
Meteor code must always run within a 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) <b>Error: Meteor code must always run within a Fiber.</b> 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
</pre>
Kyll 的评论中有解决方案:
Instead of noble.on('discover', function...) do noble.on('discover, Meteor.bindEnvironment(function...)) so that this function carries the Fiber environment of Meteor. If this solved your issue, then there's probably some duplicates around the place.
以下是有关此主题的更多信息:https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment