对于模型 \"Trains\

Cast to string failed for value \"{ details: '233' }\" at path \"setno\" for model \"Trains\

我一直在尝试查看并解决它,但最终没有找到相关的解决方案。 需要更多的意见和建议来解决这个问题。请检查我下面的代码详细信息。 我正在使用 Express GraphQL 和 mongoose 版本:^5.9.25

我正在使用 GraphQL 来做到这一点,

Below is My Query Request

query {
  getTrainsbySetno(details:"233"){
  _id
  setno
  train_no
  start_station_code
  end_station_code
  route_code
  train_type
  start_on
  change_on
  halts_at
  createdAt
  updatedAt
 }
}

My GraphqL Query Response

{
  "errors": [
  {
  "message": "Cast to string failed for value \"{ details: '233' }\" at path \"setno\" for model \"Trains\"",
  "locations": [
    {
      "line": 55,
      "column": 3
    }
  ],
  "path": [
    "getTrainsbySetno"
  ]
}
],
"data": {
 "getTrainsbySetno": null
 }
}

Below is My Resolver

getTrainsbySetno: async (details) => {
     
    try {
        const result = await Trains.find({$or:[{setno: details },{train_no: details}]});
        console.log(result);
        return result.map(res => {
           
            return { ...res._doc, _id: res.id };
            
        });            
    } catch (err) {
        
        throw err;
    }
   
},

Below is My Schema:


const Schema = mongoose.Schema;

const trainsSchema = new Schema ({

        setno : {
            type: String,
            required: true
        },
        train_no : {
            type: String,
            required: true
        },
        start_station_code : {
            type: String,
            required: false
        },
        end_station_code : {
            type: String,
            required: true
        },
        route_code : {
            type: String,
            required: true
        },
        train_type : {
            type: String,
            required: true
        },
        start_on : {
            type: String,
            required: true
        },
        change_on : {
            type: String,
            required: true
        },
        halts_at : {
            type: String,
            required: true
        }
  
    },
    { timestamps: true }
);

module.exports = mongoose.model('Trains', trainsSchema);






在getTrainsbySetno方法中,参数“details”是一个对象。因此,要获取 details obj 的值,我们需要将 details 替换为 "details.details" .