Expressjs - Mongoose:无法找到记录的属性值

时间:2015-09-08 03:02:12

标签: node.js mongodb mongoose

我遇到了一个问题,我可以访问与我的猫鼬模型对象相关联的记录,但我无法使用点表示法访问记录的属性。出于某种原因,我可以获得记录,但是当我尝试使用点符号来获取记录特定属性时,我得到undefined

这是我的模特:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var imageSchema = new Schema({
    pattern: { type: String, enum: ['solid', 'stripe', 'plaid'] },
    color: { type: String, enum: ['grey', 'navy-blue', 'black', 'khaki', 'brown'] },
    imageName: String,
    imageUrl: String,
    imageSource: String
});

var Images = mongoose.model('Images', imageSchema);

module.exports = Images;

这是我的路线定义:

router.get('/:pattern/:color/result', function(req, res){

    console.log(req.params.color);

    Images.find( { pattern: req.params.pattern, color: req.params.color }, function(err, image){
        if (err) { console.log(err); }
        console.log(image);
        console.log(image.pattern);

        res.render('pages/result.hbs', {
            pattern : req.params.pattern,
            color : req.params.color,
            image : image
        });
    });
});

console.log(image)检索如下记录:

[ { _id: 55e35b3b9bd509dbc0b46c79,
    pattern: 'solid',
    color: 'navy-blue',
    imageName: 'blueshirtesrsasdfff.jpg',
    imageUrl: 'https://imageurl.com',
    imageSource: 'source.com' } ]

console.log(image.pattern)检索此响应:

undefined

我在哪里犯了错误并致电特定财产?

1 个答案:

答案 0 :(得分:3)

using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.SystemUI; using Geoway.ADF.GIS.OpenDataDialog; using Geoway.ADF.MIS.Core.Public.interfaces; using Geoway.ADF.MIS.Core.Public.util; using Geoway.ADF.MIS.Utility.Core; using Geoway.ADF.MIS.Utility.Log; using System; using System.Collections; using System.Data; using System.Globalization; using System.IO; using System.Windows.Forms; namespace Geoway.ADF.GIS.CheckerUI { public class MapManager { private AxMapControl m_MapControl; private ICommand pControlsMapFullExtentCommand; private ICommand pControlsMapPanTool; private ICommand pControlsMapZoomInTool; private ICommand pControlsMapZoomOutTool; private ICommand pControlsMapPreViewCommand; private ICommand pControlsMapNextViewCommand; public ISimpleCallBack simAxisCallBack; public ISimpleCallBack simSelCallBack; private NumberFormatInfo nfi4 = ConvertUtil.GetNumFormatInfo(4); private ArrayList m_lstFeatureClass; private ArrayList m_lstRasterDataset; private Form parentForm; public AxMapControl MapControl { get { if (this.m_MapControl == null) { this.m_MapControl = new AxMapControl(); } return this.m_MapControl; } set { this.m_MapControl = value; } } 返回一个元素数组(您可以通过console.log中图像对象周围的方括号来表示)。如果您想让查询保持一致,您实际上需要使用.find

或者,您可以使用image[0].pattern,如此

Images.findOne