[英]Type 'string' is not assignable to type 'Condition<..>' with ts mongoose
const mongooseResponse = await mongoose.connect(mongoUri);
const game = await GameSchema.create({
image: "test",
title: "StarBurst"
})
GameSchema.findOne({title: "starburst"})
console.log(game)
I have tried to create a mongoose schema to save some data.我试图创建一个 mongoose 架构来保存一些数据。 here I just create some test data.
这里我只是创建了一些测试数据。
but an error occurs when I use the findOne
where i specify a title.但是当我使用我指定标题的
findOne
时会发生错误。
It gives me the following error:它给了我以下错误:
Type 'string' is not assignable to type 'Condition<{ type: String; unique: true; required: true; index: true; }> | undefined'.
Here is how i created my Schema:这是我创建架构的方式:
import { Document, model, Schema } from "mongoose";
export type GameModel = Document & {
image: {type: String},
title: {type: String, unique: true, required: true, index: true},
rank: {type: Number},
providerAmount: {type: Number},
nestedValues: [
{RTP: {type: String}},
{"Max Win": {type: String}},
{"Min Bet": {type: String}},
{Volatility: {type: String}},
{Betways: {type: String}},
{Release: {type: String}},
{Devices: {type: String}}
]
}
const GameSchema: Schema = new Schema({
image: {type: String},
title: {type: String, unique: true, required: true, index: true},
rank: {type: Number},
providerAmount: {type: Number},
nestedValues: [
{RTP: {type: String}},
{"Max Win": {type: String}},
{"Min Bet": {type: String}},
{Volatility: {type: String}},
{Betways: {type: String}},
{Release: {type: String}},
{Devices: {type: String}}
]
})
export default model<GameModel>('Game', GameSchema)
you have to use the model, not the schema:您必须使用 model,而不是架构:
const game = await GameModel.create({
image: "test",
title: "StarBurst"
})
let result = await GameModel.findOne({title: "starburst"})
console.log(game)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.