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

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

问题描述

在猫鼬请求之后我有我的文档 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,但前一个确实显示了一个现有的 image 属性,这意味着它存在.

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.

image 添加到您的架构中,它应该可以工作:

Add image to your schema and it should work:

image: String

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

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