【发布时间】:2019-10-19 18:43:51
【问题描述】:
打字稿抛出
“类型‘投票’不满足约束‘文档’。
'on' 的属性类型不兼容。
类型 'ObjectId' 不可分配给类型 '(event: string | symbol, listener: (...args: any[]) => void) => Vote'。
类型 'ObjectId' 不匹配签名 '(event: string | symbol, listener: (...args: any[]) => void): Vote'。”
当我使用 refPath 时。
import { Document, Schema } from 'mongoose';
interface Vote extends Document {
user: Schema.Types.ObjectId;
onModel: string;
on: Schema.Types.ObjectId;
}
export default Vote;
import mongoose, { Schema } from 'mongoose';
import Vote from '../types/Vote';
const VoteSchema: Schema = new Schema({
user: Schema.Types.ObjectId,
on: {
type: Schema.Types.ObjectId,
required: true,
refPath: 'onModel',
},
onModel: {
type: String,
required: true,
enum: ['Post', 'Comment'],
},
});
export default mongoose.model<Vote>('Vote', VoteSchema);
【问题讨论】:
标签: node.js mongodb typescript express mongoose