【问题标题】:What is the proper typing when using refPath with mongoose?将 refPath 与猫鼬一起使用时,正确的输入是什么?
【发布时间】: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


    【解决方案1】:

    编辑:

    似乎我误解了什么是错的,并且没有测试我的答案。同样在当时,我不明白他想用Document 做什么,但也没有错。

    真正的错误在于属性on、contrary to what the docs say 受到保护,不能用作架构路径名。

    将on 更改为property,或几乎任何其他密钥,都可以解决相关问题。文档中属性键的选择很不幸...

    原始问题的工作版本

    import mongoose, { Schema, Document } from 'mongoose';
    
    interface Vote extends Document {
        user: Schema.Types.ObjectId;
        propertyModel: string;
        property: Schema.Types.ObjectId;
    }
    
    const VoteSchema: Schema = new Schema({
        user: Schema.Types.ObjectId,
        property: {
            type: Schema.Types.ObjectId,
            required: true,
            refPath: 'propertyModel',
        },
        propertyModel: {
            type: String,
            required: true,
            enum: ['Post', 'Comment'],
        },
    });
    
    const Vote = mongoose.model<Vote>('Vote', VoteSchema);
    
    (async () => {
        await mongoose.connect('mongodb://localhost:27017/test', {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
    
        const vote = await Vote.create({
            user: '5f3bb78267ad4152b4079344',
            property: '5f3bb8468e770d52a4bc91f5',
            propertyModel: 'Comment',
        });
    
        await vote.save();
        await mongoose.disconnect();
    })();
    

    问题不在于refPath,而在于您正在扩展Document 并没有满足某些要求。 更具体地说:它抱怨它找不到ObjectId 属性,它不应该找到它,因为这是一个模式而不是一个文档。

    老实说,我不知道你为什么要混合 Document 和 Schema。试试这个。

    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', VoteSchema);
    

    【讨论】:

    • 我想知道,在撰写本文时,您是否测试过此代码是否有效?因为截至今天,当我尝试上述代码时。 (1) 使用 'on' 会返回错误。 (2) 会抱怨找不到onModal
    • 老实说:我没有。我从 refPath 的文档中工作,这似乎说使用 on 很好。显然情况并非如此,我已经用一个工作示例更新了答案。感谢您指出。
    • 过去两周我一直在研究参考路径,您的更新也反映了我一直在使用的内容。感谢您的更新。
    猜你喜欢
    • 2018-04-15
    • 2016-12-12
    • 2017-05-31
    • 2018-02-22
    • 2017-10-18
    • 2018-04-10
    • 2013-01-02
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多