I have been trying to see and Resolve it but ended up with no relevant solution all over. Need More Inputs and Suggestions to solve this Issue. Please Check My Below Code Details. I am Using Express GraphQL,and mongoose version :^5.9.25
I am Using GraphQL to Do that,
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);
In the getTrainsbySetno method, the parameter "details" is an object. So to get the value of the details obj we need to replace details with "details.details" .