【问题标题】:Cast to number failed for value - GraphQL转换为数值失败 - GraphQL
【发布时间】:2019-09-24 17:36:14
【问题描述】:

我是 graphQL 的新手。我正在尝试使用 graphiql,我正在尝试执行 where 我想检索年龄大于 30 岁的作者。

resolver.js

Query: {

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

    },

图形工具

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

架构模型 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;

收到此错误:

"message": "Cast to number failed for value \"{ _gte: 30 }\" at path 模型\"作者\""的\"年龄\",

【问题讨论】:

  • 您的数据库类型是什么?
  • MongoDB。我已经用模式模型更新了我的问题
  • 只需使用.find 而不是.where - mongoosejs.com/docs/api.html#model_Model.find ,您的代码就会来到authorModel.find({age: { $gte: 30 }})
  • @hoangdv 随时发布答案,以便您获得积分!非常感谢
  • @hoangdv 仅供参考:使用 where 也可以,但错误是我使用的是 _gte 而不是 $gte

标签: node.js express graphql graphql-js express-graphql


【解决方案1】:

只需使用 .find 而不是 .where - doc ,您的代码就会出现

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

或者我们可以改为写,

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

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 2020-07-22
    • 2016-01-02
    • 2023-04-09
    • 2019-06-11
    • 2011-01-09
    • 2011-05-01
    相关资源
    最近更新 更多