简体   繁体   English

类型 'string' 不能分配给类型 'Condition<..>' 与 ts mongoose

[英]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.

相关问题 Mongoose TypeScript ObjectId 转换 - 类型“字符串”不可分配给类型“条件”<objectid | undefined> '</objectid> - Mongoose TypeScript ObjectId casting - Type 'string' is not assignable to type 'Condition<ObjectId | undefined>' 打字稿错误 TS2345 错误:TS2345:“缓冲区”类型的参数不可分配给“字符串”类型的参数 - Typescript error TS2345 Error: TS2345:Argument of type 'Buffer' is not assignable to parameter of type 'string' 类型 'string[][]' 不可分配给类型 'string[]' - Type 'string[][]' is not assignable to type 'string[]' '() => Promise 类型的参数<string> ' 不可分配给 'Promise 类型的参数<string> '. TS(2345)</string></string> - Argument of type '() => Promise<string>' is not assignable to parameter of type 'Promise<string>'. ts(2345) &quot;类型 &#39;string | string[]&#39; 不可分配给类型 &#39;string&#39; - "Type 'string | string[]' is not assignable to type 'string' 类型“undefined”不可分配给类型“string | 细绳[]' - Type 'undefined' is not assignable to type 'string | string[]' TypeScript 类型'字符串 | null' 不可分配给类型 'string' - TypeScript Type 'string | null' is not assignable to type 'string' 类型'string []'不可分配给nestjs中的类型'string' - Type 'string[]' is not assignable to type 'string' in nestjs TS2345:类型X的参数不能分配给类型Y的参数 - TS2345: Argument of type X is not assignable to parameter of type Y 错误 TS2322:类型“任何”不可分配给类型“从不” - error TS2322: Type 'any' is not assignable to type 'never'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM