无法将 Meteor 集合中的数据放入数组 [英] Can't put data from a Meteor collection into an array

查看:49
本文介绍了无法将 Meteor 集合中的数据放入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Meteor,我试图将 Collection.find() 的结果传递给数组(使用变量),而我拥有的更简单的代码是(在根目录中的文件中):

I'm learning Meteor and I was trying to pass the result of a Collection.find() into and array (using a variable) and the simpler code I have is (in a file that is in the root):

    CalEvents = new Mongo.Collection('calevents');    //creating a collection 

/*------------------------- Populating the database with dummy data-------*/
    if (Meteor.isServer) {                            
    Meteor.startup(function () {
        if (CalEvents.find().count() === 0) {
          CalEvents.insert({
                title: "Initial room",
                start: '2010-02-02'
          });
        }
      });
    }
  /*--------------- Creating an array from the collection-----------------*/
    events = [];
    calEvents = CalEvents.find({});
    calEvents.forEach(function(evt){
        events.push({
            title: evt.title,
            start: evt.start,
        })
    });

页面没有什么可显示的,但使用控制台我可以看到(CalEvents.find().fetch())我的数据库中有数据但事件"变量为空...我不明白为什么,因为我尝试了其他一些事情,例如更改文件名和移动代码以保证正确的顺序.而且我已经尝试使用 CalEvents.find().fetch() 创建一个数组并将结果放入一个变量中,但我无法做到...有谁知道我错过了什么如此简单的事情?...

The page has nothing to show but using the console I can see (CalEvents.find().fetch()) that I have data in my database but the "events" variable is empty... I can't understand why because I tried several other things such as changing file names and moving code to guarantee the proper order. And I already tried to use CalEvents.find().fetch() to create an array an put the result into a variable but I'm not able to do it... Does anyone know what's so simple that I'm missing?...

推荐答案

你使用自动订阅吗?

您可能需要确保 sbscription 已准备就绪.请参阅 Meteor:如何判断数据库何时就绪?在流星收集加载时显示加载器.

You probably need to make sure the sbscription is ready. See Meteor: How can I tell when the database is ready? and Displaying loader while meteor collection loads.

您在控制台中看到 CalEvents.find().fetch() 返回项目的原因是,在您进行调用时,订阅准备就绪.但是在你的 events = [];... 代码(我假设它位于 client 目录下的文件中,您可能认为订阅数据已经到达,而实际上它没有.

The reason you do see CalEvents.find().fetch() returning items in the console is that by the time you make that call, the subscription is ready. But in your events = []; ... code (which I assume is in a file under the client directory, you might have assumed that the subscription data has arrived when in fact it has not.

一个有用的调试工具是 Chrome 的设备模式(DevTools 中搜索图标附近的电话"图标),它可以让您模拟慢速网络(例如 GPRS,每个请求有 500 毫秒的延迟).

A useful debugging tool is Chrome's device mode ("phone" icon near the search icon in DevTools), which lets you simulate slow networks (e.g. GPRS, with 500ms delay for every request).

这篇关于无法将 Meteor 集合中的数据放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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