gpt4 book ai didi

javascript - Mongoose 不返回字段

转载 作者:行者123 更新时间:2023-12-03 02:23:10 25 4
gpt4 key购买 nike

我们有与 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] ] } }

但是类型未定义!!!
我知道我可以将 lean()Query 结合起来,但我需要可变对象,而不仅仅是原始数据。

最佳答案

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

示例:

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

引用: http://mongoosejs.com/docs/guide.html#toObject

关于javascript - Mongoose 不返回字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49069592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com