客户端上的Meteor.js集合为空 [英] Meteor.js Collection empty on Client

查看:59
本文介绍了客户端上的Meteor.js集合为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 myCollection.find()。fetch()返回一个空数组 [] 尽管如果(数据){...} ,则在内进行调用? if 语句是否确保在执行 console.log()之前已检索到该集合?

Why is it that myCollection.find().fetch() returns an empty array [] even though the call is made within if(data){...}? Doesn't the if statement ensure that the collection has been retrieved before executing the console.log()?

Template.chart.rendered = function() {

        var data = myCollection.find().fetch();

        if(data) {
            console.log(data);
        }

        $('#chart').render();

}

返回 [] 在浏览器Javascript控制台中。

This returns [] in the browser Javascript console.

推荐答案

您可以使用 count()而不是返回结果的数量。 data 本身是一个空数组, [] 这不是假的( [] == true )。

You could use count() instead which returns the number of results. data itself would be an empty array, [] which isn't falsey ( [] == true ).

除非你打算使用原始数据,否则不要使用 fetch()很累。如果需要,你可以用 .forEach 循环它。

Also don't use fetch() unless you're going to use the raw data for it because its quite taxing. You can loop through it with .forEach if you need to.

var data = myCollection.find();

if(data.count())
  console.log(data);

//If you need it for something/Not sure if this is right but just an example
$('#chart').render(data.fetch())

这篇关于客户端上的Meteor.js集合为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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