【发布时间】: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