[英]Mongoose doesn't returns fields
We have NodeJs backend application which interacts with mongo database. 我们有与Mongo数据库交互的NodeJs后端应用程序。
I have strict schema for retrieve the objects. 我有严格的架构来检索对象。
NodeJs application uses module.export
technology. NodeJs应用程序使用module.export
技术。
I use follow dependencies: 我使用跟随依赖:
"mongoose": "^4.7.8",
"promise": "^8.0.1",
I have follow schema: 我有遵循架构:
account.js 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 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
}
});
Then I use the model: 然后我使用模型:
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);
}
);
I get follow in output: 我在输出中得到关注:
{ _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] ] } }
BUT types
IS UNDEFINED!!! 但是types
未定义!!!
I know that I can combine lean()
with Query
, but I need mutable oject, not just the raw data. 我知道我可以将lean()
与Query
结合使用,但是我需要可变的对象,而不仅仅是原始数据。
You can call the toObject
method in order to access the fields. 您可以调用toObject
方法以访问字段。
Example: 例:
var data = result.toObject();
console.log(data.types); // "[ 'impression', 'conversion', 'clickevent' ]"
reference: http://mongoosejs.com/docs/guide.html#toObject 参考: http : //mongoosejs.com/docs/guide.html#toObject
我发现了问题,没有在module.export
中进行module.export
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.