流星集合获取返回空数组但已订阅 [英] Meteor collection fetch returns empty array but is subscribed

查看:17
本文介绍了流星集合获取返回空数组但已订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卸载了自动订阅并重新启动了流星应用程序.从那以后,我一直无法在客户端访问我的集合数据.

每个与空数组返回相关的问题都会得到相同的答案:订阅的数据尚不可用.但无论我等多久,我都看不到客户端上的数据.

服务器:

Meteor.startup(function () {Meteor.publish("states", function () {返回 states.find();});});

在服务器上记录 states.find().fetch() 会按预期输出我的状态.

在客户端:

Meteor.subscribe("states", function(){console.log(states, states.find(), states.find().fetch());});

statesstates.find() 按预期返回对象,.fetch() 返回 []>.

等待(甚至几分钟)然后在浏览器控制台中运行 states.find().fetch() 仍然给我 [] .

想法?

编辑

集合在 isServer/isClient 块之外声明(以利用模式).

states = new Meteor.Collection("states");

解决方案

我认为你得到了 [] 因为你在启动时发布数据,当没有准备好时,让我们制作订阅反应式.

Tracker.autorun(function(){Meteor.subscribe("状态", function(){console.log(states, states.find(), states.find().fetch());});});

可选

没有理由在 isServer/isClient if 语句中声明集合

因为您是从良好实践开始的(删除 insecure/autopublish 包)

让我们做以下.

首先创建文件夹结构.(检查 meteor/structuringyourapp 和这个 SO).>

appName/lib/collection.js里面放这段代码.

states = new Meteor.Collection("states");//可选,您可以将此订阅放在 appName/client/main.js 中如果(流星.isClient){Meteor.subscribe("状态", function(){console.log(states, states.find(), states.find().fetch());});}

appName/server/publish.js

Meteor.publish("states", function () {返回 states.find();});

I uninstalled the autosubscribe and restarted the meteor app. Since then, I haven't been able to access my collection data on the client.

Every question related to the empty array return comes up with the same answer: the subscribed data isn't available yet. But no matter how long I wait I never see the data on the client.

Server:

Meteor.startup(function () {
  Meteor.publish("states", function () {
    return states.find();
  });
});

Logging states.find().fetch() on the server spits out my states as expected.

On the client:

Meteor.subscribe("states", function(){
  console.log(states, states.find(), states.find().fetch());
});

states and states.find() return objects as expected, .fetch() returns an [].

Waiting (even several minutes) then running states.find().fetch() in the browser console gives me [] still.

Thoughts?

EDIT

Collection is declared outside of the isServer/isClient blocks (to utilize schemas).

states = new Meteor.Collection("states");

解决方案

I think you are getting [] because you are publishing the data on the startup, when isn't ready, lets make that subscribe reactive.

Tracker.autorun(function(){
   Meteor.subscribe("states", function(){
      console.log(states, states.find(), states.find().fetch());
   });
});

OPTIONAL

There is no reason to declare the collections inside the isServer/isClient if statements

Since you are starting with the Good practices (removing insecure/autopublish packages)

Lets do the follow.

First Create the folder structure. (check meteor/structuringyourapp and this SO).

Inside the appName/lib/collection.js put this code.

states = new Meteor.Collection("states");
//optional you can place this subscribe inside the appName/client/main.js
if(Meteor.isClient){
   Meteor.subscribe("states", function(){
      console.log(states, states.find(), states.find().fetch());
   });
}

and on the appName/server/publish.js

Meteor.publish("states", function () {
    return states.find();
  });

这篇关于流星集合获取返回空数组但已订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆