mongoose Graphql:“消息”:“ID不能代表值:5a72240立方英尺

brgchamk  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(99)

我使用moongose在基于nodejs的应用程序上使用mongodb。目前,我尝试实现基于Grapqhl的API。
我的查询graphql模式如下所示:

const schema = buildSchema(`                                                 
    type Query {                                                            
      predictionModels(active: Boolean): [PredictionModel]
    },                                                                                   
    type PredictionModel {                                                                                                 
        _id: ID                                                                                                             
        title: String
        active: Boolean                                                                                                
    }                                                                                                      
`)

但当我用途:

query {
  predictionModels(active: true){
    _id
  }
}

我得到的回应是:

"errors": [
    {
      "message": "ID cannot represent value: 5a72240cf31713598588b70f",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "predictionModels",
        0,
        "_id"
      ]
    } ....

预测模型 Mongoose 图式:

const predictionModelSchema = new Schema({                                                                              
    title: { type: String, require: true, unique: true },                                                               
    modelId: { type: String, require: true, unique: true },                                                             
    description: { type: String, unique: true },                                                                        
    language: {type: String, enum: [ProgrammingLanguage.JS, ProgrammingLanguage.R], require: true},                     
    estimates: [{ type : Schema.Types.ObjectId, ref: 'PredictionModelEstimate'}],                                                                                             
    method: { type: methodType, require: true},                                                                         
    active: { type: Boolean, require: true}                                                                             
})
km0tfn4u

km0tfn4u1#

如果这是一个类型问题,您可以用途:

var ObjectId = require('mongoose').Types.ObjectId;
_id: new ObjectId(ID)

相关问题