Search code examples
node.jsexpressgraphqlgraphql-jsexpress-graphql

Cast to number failed for value - GraphQL


I'm new to graphQL. I'm trying to play with graphiql and I'm trying to perform a where Where I want to retrive the authors with age greater than 30.

resolver.js

Query: {

        authorGratherThan: (root, { age }) => {
            return authorModel.where({
                age: {
                    _gte: 30
                }
            });
        }

    },

graphiql tool

{
  authorGratherThan(age:30) {
    name,
    age,
  }
}

Schema model author.js

import mongoose from 'mongoose';
import uuid from 'node-uuid';
const schema = mongoose.Schema;

const authorsSchema = new schema({
    id: { type: String, default: uuid.v1 },
    name: String,
    age: Number,
    books: [ String ]
});

const model = mongoose.model('author', authorsSchema);
export default model;

Getting this error:

"message": "Cast to number failed for value \"{ _gte: 30 }\" at path \"age\" for model \"author\"",


Solution

  • Just use .find instead of .where - doc , your code will come to

    authorModel.find({age: { $gte: 30 }})
    

    or we can instead write,

    authorModel.where('age').gte(30)