简体   繁体   English

流星蒙哥未获取收集数据

[英]Meteor Mongo Not Getting Collection Data

I am trying to get a document from a collection, but it doesn't seem to be working. 我正在尝试从集合中获取文档,但是它似乎没有用。

when i use the find().fetch(), it returns only an empty array. 当我使用find()。fetch()时,它仅返回一个空数组。 my code is as follows. 我的代码如下。

var users = new Mongo.Collection("users");
console.log(users.find());
var userRecord = users.find().fetch();
var returnUserRecord = {};

if (userRecord.length >0){
    returnUserRecord = {username:userRecord.username, loginHash:userRecord.loginHash};
    console.log("if statement is not complete and the value of the return variable is");
    console.log(returnUserRecord);
}

return returnUserRecord

I have checked the database directly and noticed that there is indeed a document in the collection with the command: 我已经直接检查了数据库,发现使用命令命令集合中确实存在一个文档:

meteor mongo

if it makes any difference, all this code in the in the server js file, and is being called from from the client by: Meteor.Methods()/Meteor.call() 如果有任何区别,则所有这些代码都在服务器js文件中,并通过以下方式从客户端调用:Meteor.Methods()/ Meteor.call()

EDIT 1 编辑1

i created another collections with new data from the client, and after selecting the correct database, and running the command: 我选择了正确的数据库并运行以下命令后,从客户端使用新数据创建了另一个集合:

meteor:PRIMARY> db.newCollection1.find()

i get: 我得到:

{ "_id" : ObjectId("55d1fa4686ee75349cd73ffb"), "test1" : "asdasd", "test2" : "dsadsa", "test3" : "qweqwe" }

so this confirms that it is available in the database, but running the following in the client console, still doesnt return the result. 因此,这确认它在数据库中可用,但是在客户端控制台中运行以下命令仍不会返回结果。 (autopublish is installed. i tried removing autopublish and made the appropriate changes to subscribe to the table, but that didnt work either). (安装了autopublish。我尝试删除autopublish,并进行了适当的更改以订阅该表,但这也不起作用)。

var coll = new Meteor.Collection('newCollection1');
coll.find().fetch()

this returned an empty array. 这返回一个空数组。 i have also tried the same on the server.js code using: 我也使用以下方法在server.js代码上尝试了相同的操作:

meteor debug

but i am still getting an empty array. 但我仍然得到一个空数组。 does anyone know what i might be doing wrong here? 有人知道我在这里可能做错了吗?

SOLUTION

the solution for this was to create the collection variable in the Meteor object context. 解决方案是在Meteor对象上下文中创建collection变量。 this way it can be accessed from the Meteor context. 这样就可以从流星上下文中访问它。

ie

Meteor.coll = new Meteor.Collection('newCollection1');
Meteor.coll.find().fetch();

i hope this helps someone. 我希望这可以帮助别人。 depending on your code you may want to use a different context. 根据您的代码,您可能希望使用其他上下文。

You don't wait for this subscription to complete, therefore you get empty array. 您无需等待此订阅完成,因此会得到空数组。

You should probably read this or this to better understand it. 您可能应该阅读此书此书,以更好地理解它。

The thing is you connect users variable to "users" collection, and when you call it, it isn't yet polluted with data (if you don't want to use subscription then maybe use helper - it's reactive so it will return proper value when subscrtiption is finished) 关键是将users变量连接到“ users”集合,并且在调用它时,它尚未被数据污染(如果您不想使用订阅,则可以使用helper-它是反应性的,因此它将返回正确的值订阅结束后)

Did you subscribe your users collection somewhere? 您是否在某个地方订阅了users收藏集?

if (Meteor.isServer) {
  Meteor.publish("users", function(){
     return Users.find({})
  });
}

if (Meteor.isClient) {
  Meteor.subscribe("users");
}

First of all some advice: you can not define a collection twice. 首先一些建议:不能两次定义一个集合。 If you call new Mongo.Collection("users") a second time you will get an error. 如果第二次调用new Mongo.Collection("users") ,则会收到错误消息。 Therefore, it should be a global variable an not inside a method. 因此,它应该是全局变量,而不是方法内部。

What I can see in your code is that you are trying to use an array as if it were an object. 我在您的代码中看到的是,您试图像使用对象一样使用数组。 userRecord.username wont work because userRecord has the value of the fetch() which returns an array. userRecord.username因为userRecord具有返回数组的fetch()的值。

You could either change your code to userRecord[0].username or loop over the results with forEach like so: 您可以将代码更改为userRecord[0].username ,也可以使用forEach遍历结果,如下所示:

var users = new Mongo.Collection("users");
console.log(users.find());
users.find().forEach(function(singleUser){
    console.log(EJSON.stringyfy(singleUser));
}

in order to return the first user, you would be better of using findOne which returns the first object in the result. 为了返回第一个用户,最好使用findOne返回结果中的第一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM