You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Meteor.apply()仅订阅对应数据时才返回结果的问题求助

Meteor方法无需订阅获取数据的问题

我在Meteor React应用中,尝试使用Meteor方法从数据库获取数据并在客户端使用。方法所在文件同时在服务端和客户端运行,集合创建及方法定义文件位于myapp/imports/api目录,通过myapp/startup/server/api.js导入:

import '../../api/usersettings.js';

我清楚Meteor.call仅能在回调内部使用获取到的数据,示例如下:

Meteor.call('decorations.getOne', id, function(error, result) {
  // 在此处处理result
}); 

由于希望将返回值存入变量并在后续代码中使用,因此改用Meteor.apply,用法如下:

调用代码:

var userprice = Meteor.apply('usersettings.getOne', [o.assetCode], {returnStubValue:true, wait:true}, (err,res) => {
    if (err) {
        console.log(err);
    } else {
        return res;
    }
})

方法定义:

Meteor.methods({
    'usersettings.getOne'(assetCode) {
        if (!this.userId) {throw new Meteor.Error('Not authorised')};
        return Usersettings.findOne({ createdBy: this.userId, assetCode: assetCode});
},
...

我的问题是:只有当Meteor主动订阅了我要获取的特定文档时,该方法才会返回值,否则返回undefined。

按道理Meteor方法应该无需订阅即可工作?如果我不需要通过订阅实现数据响应式,难道不能用方法直接获取并展示数据吗?我尝试用if (Meteor.isServer) {包裹方法,但无效。


内容的提问来源于stack exchange,提问作者serkyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 09:12:36