【问题标题】:Graphql: "message": "ID cannot represent value: 5a72240cf31713598588b70f"Graphql:“消息”:“ID不能代表值:5a72240cf31713598588b70f”
【发布时间】:2019-03-28 02:13:07
【问题描述】:

我使用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"
      ]
    } ....

预测模型猫鼬模式:

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}                                                                             
})

【问题讨论】:

  • 如果为_id 提供的值既不是字符串也不是整数,则会显示此消息。 Mongoose 对象 id 字段应由 graphql-js 由this function 正确序列化。你能添加你的猫鼬模型吗?

标签: mongodb mongoose graphql


【解决方案1】:

如果这是类型问题,您可以使用:

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

【讨论】:

    猜你喜欢
    • 2021-03-07
    • 2020-05-18
    • 2015-09-09
    • 1970-01-01
    • 2017-11-26
    • 2023-03-13
    • 2018-02-10
    • 2017-06-02
    • 2022-01-15
    相关资源
    最近更新 更多