Meteor 中 chokidar 的使用示例,以防止错误:Meteor 代码必须始终在光纤中运行



下面的代码给了我一个错误:"流星代码必须始终在光纤中运行。尝试使用 Meteor.bindEnvironment 包装传递给非 Meteor 库的回调。

import chokidar from 'chokidar';
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
const Plates = new Mongo.Collection('plates');
var path = '~/Documents/dev/lpr/test';
var watcher = chokidar.watch(path, {
ignored: /(^|[/\])../,
persistent: true
});
watcher.on('add', path => {
console.log(`DEBUG: File ${path} has been added`);
Plates.insert({
path,
})
});

Meteor文档(https://guide.meteor.com/using-npm-packages.html#wrap-async(建议使用Meteor.wrapAsync来解决此问题,但我不明白在这种情况下如何应用?

例如,下面返回"TypeError: watcherFiber.on 不是一个函数">

var watcherFiber = Meteor.wrapAsync(chokidar.watch(path, {
ignored: /(^|[/\])../,
persistent: true
}));
watcherFiber
.on('add', path => {
console.log(`DEBUG: File ${path} has been added`);
Plates.insert({
path,
})
});

错误文本为您指出了执行此操作的正确方法。

它应该是这样的:

watcher.on('add', Meteor.bindEnvironment(path => {
console.log(`DEBUG: File ${path} has been added`);
Plates.insert({
path,
});
}));

最新更新