Mongoose Schema:为模式分配 Typescript 接口

发布于 2025-01-17 21:17:36 字数 2282 浏览 3 评论 0原文

我想使用接口键入杂种模式。这样:

const mongoose = require('mongoose')
const { Schema, Model, Document } = mongoose;

interface WordInterface extends Document {
    _id: string | null;
    wordType: WordTypes;
    text: string;
    verses: VerseFromTo[] | null;
    createdAt: number;
    modifiedAt: number;
    creator: string;
}

const wordSchema = new Schema({
    _id: { type: String },
    wordType: { type: Object as () => WordTypes, required: true },
    text: { type: String, required: true },
    verses: { type: Object as () => VerseFromTo[] },
    createdAt: { type: Number, required: true },
    modifiedAt: { type: Number, required: true },
    creator: { type: String, required: true }
});

const words: typeof Model<WordInterface> = mongoose.model('words', wordSchema);

但是在这条线中,我会遇到一个错误:

const emotions: typeof Model<WordInterface> = mongoose.model('emotions', emotionSchema);
                            ^               ^

上面的行下方的上箭头显示了两个绒毛错误。我是打字稿的菜鸟,所以我可能会误解该语法。错误都是: ts1005:','预期。

我无法弄清楚我做错了什么。我尝试了这些文档中的示例:

我也尝试输入模式,但它行不通:

const wordSchema = new Schema<WordInterface>({

但是它给出了以下错误:

TS2347: Untyped function calls may not accept type arguments.

卡住!非常感谢;)

  "dependencies": {
    "@types/body-parser": "^1.19.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.10.7"
  },
  "devDependencies": {
    "@types/express": "^4.17.8",
    "@typescript-eslint/eslint-plugin": "^5.16.0",
    "@typescript-eslint/parser": "^5.16.0",
    "eslint": "^8.12.0",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.3",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  }

I want to use an interface to type a Mongoose Schema. Like this:

const mongoose = require('mongoose')
const { Schema, Model, Document } = mongoose;

interface WordInterface extends Document {
    _id: string | null;
    wordType: WordTypes;
    text: string;
    verses: VerseFromTo[] | null;
    createdAt: number;
    modifiedAt: number;
    creator: string;
}

const wordSchema = new Schema({
    _id: { type: String },
    wordType: { type: Object as () => WordTypes, required: true },
    text: { type: String, required: true },
    verses: { type: Object as () => VerseFromTo[] },
    createdAt: { type: Number, required: true },
    modifiedAt: { type: Number, required: true },
    creator: { type: String, required: true }
});

const words: typeof Model<WordInterface> = mongoose.model('words', wordSchema);

But in this line, I get an error:

const emotions: typeof Model<WordInterface> = mongoose.model('emotions', emotionSchema);
                            ^               ^

There are two linting errors shown by the up-arrows beneath the line above. I'm a noob to typescript so I may be misunderstanding the syntax. The errors are both:
TS1005: ',' expected.

I can't work out what I've done wrong. I tried examples from these docs:

I also tried typing the Schema but it won't work:

const wordSchema = new Schema<WordInterface>({

But it gave the following error:

TS2347: Untyped function calls may not accept type arguments.

Stuck! Help very much appreciated ;)

  "dependencies": {
    "@types/body-parser": "^1.19.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.10.7"
  },
  "devDependencies": {
    "@types/express": "^4.17.8",
    "@typescript-eslint/eslint-plugin": "^5.16.0",
    "@typescript-eslint/parser": "^5.16.0",
    "eslint": "^8.12.0",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.3",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文