当我使用字符串文字时,mongoose findById起作用,但是当我引用Object的属性时,mongoose findById不起作用 [英] mongoose findById works when I use a string literal but not when I reference a property of an Object

查看:89
本文介绍了当我使用字符串文字时,mongoose findById起作用,但是当我引用Object的属性时,mongoose findById不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在猫鼬中遇到了一个非常奇怪的问题.

I'm having a really strange problem in Mongoose.

此行正确找到Round:

models.Round.findById("555ec731385b4d604356d8e5", function(err, roundref){
            console.log(roundref);
            ....

此行不

models.Round.findById(result.round, function(err, roundref){
            console.log(roundref);

我已经在控制台上登录了result,它显然是一个包含回合属性的对象:

I've console logged result and it clearly is an object containing the property round:

{round: "555ec731385b4d604356d8e5", selection: 1, time: 20}

为什么findById没有文字就不能工作?

Why won't findById work without a literal?

推荐答案

如果result是JSON字符串,则调用.round将返回undefined.

If result is a JSON string, calling .round would return undefined.

尝试首先将JSON转换为javascript对象:

Try converting the JSON to a javascript object first:

result = JSON.parse(result);
models.Round.findById(result.round, function(err, roundref){
        console.log(roundref);

这篇关于当我使用字符串文字时,mongoose findById起作用,但是当我引用Object的属性时,mongoose findById不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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