Mongoose为现有字段返回undefined [英] Mongoose returns undefined for an existing field

查看:487
本文介绍了Mongoose为现有字段返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mongoose请求之后我有我的文件 doc 这是查询的结果

After a mongoose request I have my document doc which is the result of the query

这是使用的模式

var searchSchema = new mongoose.Schema({
    original : String,
    images : [String],
    image: String
});

型号:

var searchModel = mongoose.model('Search', searchSchema);

使用的代码:

searchModel.findOne({original : input}, function (err, doc) {
    if (err) {
        console.log(err);
    }
    if (typeof doc !== "undefined") {
        console.log(doc);
                    console.log(doc.image);
    }
});

第一个 console.log

{ 
    _id: 531401bf714420359fd929c9,
    image: 'http://url.com/image.jpg',
    original: 'lorem ipsum dolor sit amet' 
}

第二个返回 undefined ,但前一个确实显示现有的图像属性,这意味着它存在。

The second returns undefined, but the previous one does show an existing image property, which means that it exists.

我的架构没有什么特别之处,所以我不明白这里可能会发生什么..

My schema doesn't have anything special so I don't understand what may be happening here..

推荐答案

当你没有将字段添加到架构时,你会看到这一点。

You'll see this when you haven't added the field to your schema.

添加图像到你的架构,它应该工作:

Add image to your schema and it should work:

image: String

这篇关于Mongoose为现有字段返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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