Javascript Mongoose不返回字段

Javascript Mongoose不返回字段,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我们有与mongo数据库交互的NodeJs后端应用程序。 我有检索对象的严格模式。 NodeJs应用程序使用module.export技术 我使用以下依赖项: "mongoose": "^4.7.8", "promise": "^8.0.1", 我有以下计划: account.js const mongoose = require('mongoose'), ColumnModel = require('./column'); var AccountModel = new

我们有与mongo数据库交互的NodeJs后端应用程序。 我有检索对象的严格模式。 NodeJs应用程序使用module.export技术

我使用以下依赖项:

"mongoose": "^4.7.8",
"promise": "^8.0.1",
我有以下计划:

account.js

const   mongoose = require('mongoose'),
        ColumnModel = require('./column');

var AccountModel = new mongoose.Schema({
    id: {
        type: Number,
        required: [true, "ID is required"],
        unique: true
    },
    maxAvailableDate: {
        type: Date,
        require: [true, "maxAvailableDate is required"]
    },
    types: {
        type: [String],
        required: [true, "Log types are required"]
    },
    metaInfo: {
        conversion: {
            type: [ColumnModel],
            required: [true, "Conversion columns are required"]
        },
        impression: {
            type: [ColumnModel],
            required: [true, "Impression columns are required"]
        },
        clickevent: {
            type: [ColumnModel],
            required: [true, "Clickevent columns are required"]
        }
    }
});
column.js

const   mongoose = require('mongoose');

module.exports = new mongoose.Schema({
    columnName: {
        type: String,
        required: true
    },
    columnType: {
        type: String,
        enum: [
            "string", 
            "date",
            "int",
            "real"
        ],
        required: true
    }
});
然后我使用模型:

const AccountModel = require('./account'),
      mongoose = require('mongoose');

let AccountModel = mongoose.model('Account', AccountModel);

AccountModel.findOne(
    {id: accountId},
    (err, result) => {
        let types = result.types;
        console.log(result);
    }
);
我得到以下输出:

{ _id: 5a98116ff7f1e223e5b291fa,
  id: 119,
  maxAvailableDate: 2018-03-01T14:42:55.262Z,
  types: [ 'impression', 'conversion', 'clickevent' ],
  metaInfo: 
   { conversion: 
      [ [Object],
        [Object] ],
     impression: 
      [ [Object],
        [Object] ],
     clickevent: 
      [ [Object],
        [Object] ] } }
但是类型是未定义的!!! 我知道我可以将精益与查询相结合,但我需要的是可变项目,而不仅仅是原始数据。

您可以调用toObject方法来访问字段

例如:

var data = result.toObject();
console.log(data.types); // "[ 'impression', 'conversion', 'clickevent' ]"
参考:

我发现了问题,我没有在accounts.js中module.export