gpt4 book ai didi

meteor :Deps.autorun 内的 collection.find().fetch() 失败

转载 作者:行者123 更新时间:2023-12-01 10:46:53 25 4
gpt4 key购买 nike

我想在 meteor 服务器上创建一个响应式(Reactive)集合查询,并在有变化时处理结果。

这是我的代码:

if (Meteor.isServer) {
Meteor.startup(function(){
Deps.autorun(function(){
var items=new Meteor.Collection('name').find().fetch();
// ... process the items ...
});
});
}

(事实上,对于这个测试,整个项目只是在一个 .js 文件中包含上述代码)。用 meteor 启动这个程序会抛出一个错误:

/home/yaakov/Bug/.meteor/local/build/programs/server/boot.js:198
}).run();
^
Error: Can't call yield in a noYieldsAllowed block!
at Function.Fiber.yield (packages/meteor/fiber_helpers.js:11)
at Function.wait (home/yaakov/.meteor/tools/cef2bcd356/lib/node_modules/fibers/future.js:111:14)
at Object.Future.wait (/home/yaakov/.meteor/tools/cef2bcd356/lib/node_modules/fibers/future.js:325:10)
at new MongoConnection (packages/mongo-livedata/mongo_driver.js:196)
at new MongoInternals.RemoteCollectionDriver (packages/mongo-livedata/remote_collection_driver.js:4)
at Object.<anonymous> (packages/mongo-livedata/remote_collection_driver.js:44)
at Object.defaultRemoteCollectionDriver (packages/underscore/underscore.js:750)
at new Meteor.Collection (packages/mongo-livedata/collection.js:72)
at app/Bug.js:4:17
at packages/deps/deps.js:47

我做错了什么?如何在 meteor 服务器上创建响应式(Reactive)集合查询?

最佳答案

Deps 包只能在客户端工作,所以你不能在服务器端使用Deps.autorun

要在服务器上使用类似响应式的查询,请改用observe:

var items=collection.find().observe({
added: function (document) {
// ...
},
changed: function (newDocument, oldDocument) {
// ...
},
removed: function (oldDocument) {
// ...
}
});

关于 meteor :Deps.autorun 内的 collection.find().fetch() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25204314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com