猫鼬.find字符串参数 [英] Mongoose .find string parameters

查看:91
本文介绍了猫鼬.find字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用猫鼬查询我的数据库的功能.该连接已经有效.但是,如果我不使用实际的字符串,而是使用字符串变量,则.find将返回未定义的对象(即找不到任何对象).这是代码:

I have a function using mongoose to query my database. The connection already works. However, if I do not use actual strings, but instead string variables, the .find returns an undefined object (i.e. it doesn't find anything). Here is the code:

function getUser(obj, callback){
    console.log("obj.key: " + obj.key[0].toString() + ":" + obj.value[0].toString());
    var first = "name.first";
    console.log("first: " + first);
    var second = "nicholas";
    console.log("second: " + second);
    User.findOne({first:second}, 'name telephone address1 address2 city state zip country', function(err, user){//nothing can be found when not directly using a string!!!
        console.log("got this user: " + user.name);

    });
}

这不起作用,但是,如果我用.find替换该行,它将起作用:

this does not work, however, if I replace the line with .find with this, it does:

User.findOne({"name.first":"nicholas"}, 'name telephone address1 address2 city state zip country', function(err, user){

我以前从未见过这样的事情.通常,字符串是一个字符串,无论您用它做什么,它都可以工作. 任何想法可能有什么问题吗?

I've never seen anything like this before. Normally a string is a string and it will work no matter what you do with it. Any ideas what might be wrong?

p.s.console.logs:

p.s.the console.logs:

obj.key:name.first:nicholas
first:name.first
second: nicholas
错误:console.log(让该用户...无法读取 属性名称"为空.

obj.key: name.first:nicholas
first: name.first
second: nicholas
error at: console.log("got this user...cannot read property 'name' of null.

推荐答案

按照 JavaScript对象文字语法,用符号{first:second}表示,first属性名称,而不是变量可以扩展

As per JavaScript object literal syntax, in the notation {first:second}, first is a property name, not a variable subject to expansion.

引用文档:

对象属性名称可以是任何字符串,包括空字符串. 如果属性名称不是有效的JavaScript标识符,则必须将其用引号引起来.不是有效标识符的属性名称也不能作为点(.)属性访问,但可以访问并设置为类似数组的符号("[]").

Object property names can be any string, including the empty string. If the property name would not be a valid JavaScript identifier, it must be enclosed in quotes. Property names that would not be valid identifiers also cannot be accessed as a dot (.) property, but can be accessed and set with the array-like notation("[]").


给您一个问题:


Given you question:

  • User.findOne({"name.first":"nicholas"}, ...)可以用作您的属性命名为name.first(这是使用点符号)
  • User.findOne({first:"nicholas"}, ...)无效,因为您引用的是名为first
  • 的字段
  • User.findOne({"name.first":"nicholas"}, ...) works as your property is named name.first (this is a MongoDB property using the dot notation)
  • User.findOne({first:"nicholas"}, ...) does not work as you are refering to a field named first

您可以通过使用[]初始化对象字段来获得所需的结果:

You may achieve the desired result by initializing your object field using []:

query = {}
query[first] = "nicholas"
//    ^^^^^
//  this is a variable
User.findOne(query, ...)

请注意,由于这是一个经常性需求,因此ECMA 6具有对象文字计算属性键,使您可以编写类似于 的内容:

Please note as this is a recurrent need, ECMA 6 has object literal computed property keys allowing you to write something like that:

{ [first]: "nicholas" }

AFAIK,但到目前为止,主要的JS实现对此并没有多大支持.

AFAIK, as of today this is not much supported by the major JS implementation though.

这篇关于猫鼬.find字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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