【问题标题】:Type 'string' is not assignable to type 'Condition<..>' with ts mongoose类型 'string' 不可分配给类型 'Condition<..>' 与 ts mongoose
【发布时间】:2021-04-21 21:58:51
【问题描述】:
const mongooseResponse = await mongoose.connect(mongoUri);


const game = await GameSchema.create({
  image: "test",
  title: "StarBurst"
})

GameSchema.findOne({title: "starburst"})
console.log(game)

我试图创建一个猫鼬模式来保存一些数据。这里我只是创建了一些测试数据。

但是当我使用findOne 指定标题时发生错误。

它给了我以下错误:

Type 'string' is not assignable to type 'Condition&lt;{ type: String; unique: true; required: true; index: true; }&gt; | undefined'.

这是我创建架构的方式:

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)

【问题讨论】:

    标签: node.js mongodb typescript mongoose


    【解决方案1】:

    你必须使用模型,而不是架构:

        const game = await GameModel.create({
      image: "test",
      title: "StarBurst"
    })
        
       let result = await GameModel.findOne({title: "starburst"})
        console.log(game)
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2020-07-05
      • 1970-01-01
      • 2021-12-10
      • 2021-09-26
      • 2019-11-15
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多