我仍在尝试学习graphql,并且在访问数据库中对象内的项时遇到问题。在我的客户端代码中,id和createdAt的数据显示得很好——只是当我添加对象时,我才得到错误:
Expected Iterable, but did not find one for field Users.profile
我不确定我的代码缺少什么:
解析器:
Query: {
getUser(root, args, { userId }) {
const {id } = args;
const user = User.findOne({
id
});
return user;
}
},
模式
const User = `
type User{
id: String!
createdAt: Date
profile: [Profile]
}
type Profile {
name: String!
email: String!
}
extend type Query {
getUser(
id: String!
): User
}
我是如何在我的客户端代码中调用它的:
const getUser = gql`
query getUser($id: String!) {
getUser(id: $id) {
id
createdAt
profile {
name
email
}
}
}
`;
这就是它在MongoDB数据库中的样子:
user{
_id: "22222"
createdAt: 11/22/2018
profile:{
name: "Chris"
email: "chris@emample.com"
}
} `
如果这对将来的人有帮助,我必须将我的对象设置为JSON才能使其工作。
const User = `
type User{
id: String!
createdAt: Date
profile: JSON
}
extend type Query {
getUser(
id: String!
): User
}