Mongoose Schema:为模式分配 Typescript 接口
我想使用接口键入杂种模式。这样:
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:','预期。
我无法弄清楚我做错了什么。我尝试了这些文档中的示例:
- https://mongoosejs.coms.com/docs/docs/dypescript.html
- https://thecodebarrain.com/working-with-with-with-with-with-with-mongoose-ingoose-in-typescript。 html
我也尝试输入模式,但它行不通:
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:
- https://mongoosejs.com/docs/typescript.html
- https://thecodebarbarian.com/working-with-mongoose-in-typescript.html
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论