从 Meteor 集合中检索数据 [英] Retrieving Data from Meteor Collections

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

问题描述

我在尝试从 Meteor 集合中获取数据时遇到了一些问题,我需要一些建议.

I'm having a few problems when trying to get data from a Meteor Collection and I need some advice.

已成功定义、发布和订阅该集合.如果我将数据发送到模板,它显示正常:

The collection has been defined, published, and subscribed successfully. If I send the data to a template, it displays fine:

Template.Lists.Projects = function(){
    return Projects.find();
};

但我试图在显示数据之前使用它,这就是我遇到问题的地方.首先,我发现 find() 和 findOne() 之间有些不一致.find(selector) 工作正常并返回一个游标,但 findOne(selector) 返回未定义".我真的只是在寻找 1 个条目,所以 find() 似乎没有必要.

But I am trying to use the data before displaying it, and this is where I run into problems. First, I'm getting some inconsistencies between find() and findOne(). find(selector) works fine and returns a cursor, but findOne(selector) returns "undefined." I really am only looking for 1 entry, so find() seems unnecessary.

返回 LocalCollection.Cursor:

Returns LocalCollection.Cursor:

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);

返回未定义:

var find = Projects.findOne({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);

在 LocalCollection.Cursor 上使用 .fetch() 时出现了我的下一个问题.它返回一个空数组.

My next problem arises when using .fetch() on the LocalCollection.Cursor. It returns an empty array.

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch);

所有这些返回的是以下行:

all this returns is the following line:

[ ]

当我尝试从要显示的数组中指定特定键时,例如:

When I try to specify a specific key from the array I want to display, like:

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch.name);

它返回未定义.

我仍在熟悉 Meteor 并且从未使用过 MongoDB(或 minimongo),所以我可能只是犯了一些愚蠢的错误.如果有人能向我指出,我会很高兴!

I'm still familiarizing myself with Meteor and have never used MongoDB (or minimongo), so I'm probably just making some stupid mistake. If anyone can point it out to me I would be thrilled!

推荐答案

您对 find() 和 findOne() 的结果是一致的.基本上,Mongo 或 minimongo 根本找不到与该 _id 匹配的文档.FindOne() 就像做一个 find(selector, options).fetch()[0].

Your results for find() and findOne() are consistent. Basically, Mongo or minimongo is simply not finding a document that matches that _id. FindOne() is precisely like doing a find(selector, options).fetch()[0].

您的 Lists.Projects 模板可能需要一个可以迭代的集合、数组或散列.您不能退回一份特定文件.如果您使用的是 {{#each Projects}},则必须为模板提供某种方式来迭代而不只是单个值.

Your Lists.Projects template is likely expecting a collection, array or hash it can iterate over. You cannot return one specific document. If you are using {{#each Projects}} you must provide some way for the template to iterate not just a single value.

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

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