如何访问在Meteor中从Mongo查询的对象的属性 [英] How do you access attributes of an object queried from Mongo in Meteor

查看:36
本文介绍了如何访问在Meteor中从Mongo查询的对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Meteor.js框架中的mongo还是很陌生.在这里,我已经使用它的ID查询了MongoDB对象.我正在尝试访问属性法力",但它使我返回未定义状态.

I'm pretty new to mongo in the Meteor.js framework. Here, I've queried a MongoDB object using it's ID. I'm trying to access an attribute "mana" but it returns me undefined.

var skill = Skills.find({_id:Session.get("selected_skill")});
  alert(skill);//This returns me "undefined"
  Skills.update(Session.get("selected_skill"), {$inc: {mana: 1}});

您能启发我对在mongo中访问流星的属性的要求吗?谢谢

Could you enlighten me on the requirements of accessing attributes in mongo for meteor? Thanks

推荐答案

find方法返回游标,而不是对象或数组.要访问对象,您需要从游标中获取对象

find method returns a cursor, not object nor array. To access object, you need to either fetch it from the cursor

var skill = Skills.find(Session.get('selected_skill')).fetch()[0];

或直接通过findOne获取:

var skill = Skills.findOne(Session.get('selected_skill'));

然后,您可以像使用其他任何js对象一样使用它:

Then you may use it just as any other js object:

console.log(skill.mana);
skill._cache = {cooldown: true};

 

 

请记住,在客户端,find之类的收集方法是非阻塞的.它们返回Meteor在缓存中拥有的内容,而不必返回服务器端数据库中的内容.这就是为什么您应该始终在反应性上下文中使用它们,或者确保在执行之前已获取所有数据(在您精通Meteor之前,不必担心后者),

Keep in mind that on client-side, collection methods like find are non-blocking. They return whatever Meteor has in cache, not necessarily what is in the server-side db. That's why you should always use them in a reactive context, or ensure that all data has been fetched before execution (don't worry about the latter until you're fluent with Meteor, start with the first way).

此外,您需要牢记,因此,即使相应的元素位于db中(但尚未缓存),findOnefind.fetch仍可能返回null/空数组.如果您在反应式功能中没有考虑到这一点,则会遇到错误.

Also, you need to keep in mind that because of this, findOne and find.fetch may return null / empty array, even when the corresponding element is in db (but hasn't been yet cached). If you don't take that into account in your reactive functions, you'll run into errors.

Template.article.slug = function() {
    var article = Articles.findOne(current_article);
    if(!article) return '';
    return slugify(article.title);
};

如果我们没有使用if(!article)从函数中转义,则表达式article.title会在第一次计算中引发错误,因为article将是未定义的(假定它没有被更早地缓存).

If we didn't escape from the function with if(!article), the expression article.title would raise an error in the first computation, as article would be undefined (assuming it wasn't cached earlier).

 

 

要从客户端更新数据库时,一次只能更改一项,并且必须通过其_id引用该项目.这是由于安全原因.您对此行还可以:

When you want to update the database from client side, you can alter only one item by a time, and you must refer to the item by its _id. This is due to security reasons. Your line for this was ok:

Skills.update(Session.get('selected_skill'), {$inc: {mana: 1}});

 

 

alert()是一个函数,无论您提供什么内容,它都会返回未定义.

alert() is a function that returns undefined no matter what you feed it.

alert(42); // -> undefined

通常,与alert相比,console.log进行调试要好得多.

Generally, it's far better to debug with console.log than with alert.

这篇关于如何访问在Meteor中从Mongo查询的对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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