首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'string‘不能赋值给带有ts mongoose的类型'Condition<..>’

类型'string‘不能赋值给带有ts mongoose的类型'Condition<..>’
EN

Stack Overflow用户
提问于 2021-01-17 20:07:44
回答 1查看 1.2K关注 0票数 0
代码语言:javascript
复制
const mongooseResponse = await mongoose.connect(mongoUri);


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

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

我尝试创建一个mongoose模式来保存一些数据。在这里,我只创建一些测试数据。

但是当我在指定标题的地方使用findOne时出现错误。

它给出了以下错误:

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

下面是我创建模式的方式:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-17 20:29:09

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

代码语言:javascript
复制
    const game = await GameModel.create({
  image: "test",
  title: "StarBurst"
})
    
   let result = await GameModel.findOne({title: "starburst"})
    console.log(game)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65760377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档