客户端上的Meteor.js集合为空

时间:2021-07-14 17:28:04

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()?

为什么myCollection.find()。fetch()返回一个空数组[],即使调用是在if(data){...}中进行的?在执行console.log()之前,if语句是否确保已检索到该集合?

Template.chart.rendered = function() {

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

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

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

}

This returns [] in the browser Javascript console.

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

4 个解决方案

#1


6  

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

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

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.

除非你要使用原始数据,否则不要使用fetch(),因为它非常繁琐。如果需要,可以使用.forEach遍历它。

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())

#2


2  

It seems when you "remove autopublish" you have to also subscribe on the client.

看来当你“删除自动发布”时,你还必须在客户端订阅。

  if(Meteor.isClient) {
    Meteor.startup(function() {
      Myvars = new Mongo.Collection("myvars");
      Meteor.subscribe('myvars')
    });
  }

and enable allow and publish on the server

并在服务器上启用允许和发布

  if(Meteor.isServer) {
    Meteor.startup(function () {
      Myvars = new Mongo.Collection("myvars");
      Myvars.allow({
        insert: function () {
          return true;
        },
        update: function () {
          return true;
        },
        remove: function () {
          return true;
        }
      });
      if (Myvars.find().count() == 0) {
        Myvars.insert({myvalue:'annoyed'});
      }

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

I'm new to this as well. I was just looking to have a global value that all clients could share. Seems like a useful idea (from a beginner's perspective) and a complete oversight on the Meteor teams behalf, it was nowhere clearly documented in this way. I also still have no idea what allow fetch is, that too is completely unclear in the official documentation.

我也是新手。我只是希望拥有一个所有客户都可以分享的全球价值。看起来像一个有用的想法(从初学者的角度来看)和代表Meteor团队的完全监督,它没有以这种方式清楚地记录。我还不知道什么允许获取,在官方文档中也完全不清楚。

#3


1  

It does, but in javascript you have the following strange behaviour

确实如此,但在javascript中你有以下奇怪的行为

if ([]){
  console.log('Oops it goes inside the if')
} // and it will output this, nontheless it is counter-intuitive

This happens because JS engine casts Boolean([]) to true. You can how different types are casted to Boolean here.

发生这种情况是因为JS引擎将Boolean([])强制转换为true。您可以在此处将不同类型的类型转换为布尔值。

Check if your array is not empty in the beginning.

检查您的数组在开始时是否为空。

a = [];
if (a.length){
  //do your thing
}

#4


1  

The problem is that you have to wait for data from the server. When you just use Template.name.rendered function it is immediately invoked. You have to use Template.name.helpers function to wait for data from the server. Everything is described in the documentation.

问题是您必须等待来自服务器的数据。当您只使用Template.name.rendered函数时,会立即调用它。您必须使用Template.name.helpers函数来等待来自服务器的数据。文档中描述了一切。

#1


6  

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

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

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.

除非你要使用原始数据,否则不要使用fetch(),因为它非常繁琐。如果需要,可以使用.forEach遍历它。

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())

#2


2  

It seems when you "remove autopublish" you have to also subscribe on the client.

看来当你“删除自动发布”时,你还必须在客户端订阅。

  if(Meteor.isClient) {
    Meteor.startup(function() {
      Myvars = new Mongo.Collection("myvars");
      Meteor.subscribe('myvars')
    });
  }

and enable allow and publish on the server

并在服务器上启用允许和发布

  if(Meteor.isServer) {
    Meteor.startup(function () {
      Myvars = new Mongo.Collection("myvars");
      Myvars.allow({
        insert: function () {
          return true;
        },
        update: function () {
          return true;
        },
        remove: function () {
          return true;
        }
      });
      if (Myvars.find().count() == 0) {
        Myvars.insert({myvalue:'annoyed'});
      }

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

I'm new to this as well. I was just looking to have a global value that all clients could share. Seems like a useful idea (from a beginner's perspective) and a complete oversight on the Meteor teams behalf, it was nowhere clearly documented in this way. I also still have no idea what allow fetch is, that too is completely unclear in the official documentation.

我也是新手。我只是希望拥有一个所有客户都可以分享的全球价值。看起来像一个有用的想法(从初学者的角度来看)和代表Meteor团队的完全监督,它没有以这种方式清楚地记录。我还不知道什么允许获取,在官方文档中也完全不清楚。

#3


1  

It does, but in javascript you have the following strange behaviour

确实如此,但在javascript中你有以下奇怪的行为

if ([]){
  console.log('Oops it goes inside the if')
} // and it will output this, nontheless it is counter-intuitive

This happens because JS engine casts Boolean([]) to true. You can how different types are casted to Boolean here.

发生这种情况是因为JS引擎将Boolean([])强制转换为true。您可以在此处将不同类型的类型转换为布尔值。

Check if your array is not empty in the beginning.

检查您的数组在开始时是否为空。

a = [];
if (a.length){
  //do your thing
}

#4


1  

The problem is that you have to wait for data from the server. When you just use Template.name.rendered function it is immediately invoked. You have to use Template.name.helpers function to wait for data from the server. Everything is described in the documentation.

问题是您必须等待来自服务器的数据。当您只使用Template.name.rendered函数时,会立即调用它。您必须使用Template.name.helpers函数来等待来自服务器的数据。文档中描述了一切。