【发布时间】:2021-07-25 02:11:55
【问题描述】:
我使用 Typescript 来获取对象
const query = Workflow.where({ _id: id });
const workflow = await query.findOne().exec();
console.log(workflow); // <-- (1) this console.log()
console.log(workflow.id); // <-- (2) this console.log()
return workflow;
然后我得到:
{
_id: 608e7fb7ee267e23fff1da65,
title: 'WorkFlow 14',
description: 'Este es un nuevo workflow',
date: 2021-05-02T10:32:23.423Z,
__v: 0
}
608e7fb7ee267e23fff1da65
正如您所注意到的,我在第一个 console.log 中将 _id 作为属性,但是当我在 console.log workflow.id 时得到结果。
我的架构
import mongoose from 'mongoose';
const WorkflowSchema = new mongoose.Schema({
title: { type: String, required: true },
description: { type: String, required: false },
date: { type: Date, default: Date.now },
});
export default mongoose.model('Workflow', WorkflowSchema);
这只发生在 id 属性上。
为什么有效?我预计至少会出现错误或undefined。
【问题讨论】:
-
也许有一些别名或打字稿功能?
标签: typescript mongoose