node.js - “typeof Schema”类型上不存在属性“virtual”
问题描述
我正在尝试将 _id 从 mongoose 模式更改为 'id' 就像这里显示的MongoDB:输出 'id' 而不是 '_id'
复制 ID 字段。
Schema.virtual('id').get(function(){
return this._id.toHexString();
});
// Ensure virtual fields are serialised.
Schema.set('toJSON', {
virtuals: true
});
我正在使用打字稿,而 Schema 似乎没有“虚拟”方法或“设置”方法,并且关键字“this”也未绑定在此上下文中。谁知道他们的打字稿等价物?
解决方案
这似乎适用于继承
import { Schema } from 'mongoose';
class BaseSchema extends Schema {
constructor(args) {
super();
this.add(args);
this.virtual('id').get(function(this: any) {
return this._id.toHexString();
});
this.set('toObject', {
virtuals: true,
});
this.set('toJSON', {
virtuals: true,
});
}
}
推荐阅读
- c++ - Cython: Invalid index type 'int'
- java - 频率攻击,尝试打印时字符显示不正确(上为下,下为上)
- google-cloud-platform - 如何在对话流中重定向到与用户输入关键字相关的意图
- git - GitLab SSH 权限被拒绝(公钥)
- javascript - 找不到 Barba 包装纸
- testing - 对多个构建代理进行端到端测试?
- java - OnClickListener中的Android,通过方法部分更新图像
- lua - ReplicatedFirst:RemoveDefaultLoadingScreen() 未在 Studio 中触发
- python-3.x - 如何使用 Google Calendar API 创建“全天”事件
- javascript - 表单提交时页面重新加载!Rails API 后端 Javascript 前端