【发布时间】:2021-04-30 02:12:42
【问题描述】:
在服务器上,我正在填充用户数据,当我将其打印到控制台时一切正常,但我无法访问客户端上的数据,甚至无法访问 Playground 的 GraphQL 上的数据。
这是我的架构
const { model, Schema } = require("mongoose");
const postSchema = new Schema({
body: String,
user: {
type: Schema.Types.ObjectId,
ref: "User",
},
});
module.exports = model("Post", postSchema);
const userSchema = new Schema({
username: String,
});
module.exports = model("User", userSchema);
const { gql } = require("apollo-server");
module.exports = gql`
type Post {
id: ID!
body: String!
user: [User]!
}
type User {
id: ID!
username: String!
}
type Query {
getPosts: [Post]!
getPost(postId: ID!): Post!
}
`;
Query: {
async getPosts() {
try {
const posts = await Post.find()
.populate("user");
console.log("posts: ", posts[0]);
// This works and returns the populated user with the username
return posts;
} catch (err) {
throw new Error(err);
}
},
}
但在客户端甚至 Playground 中,我无法访问填充的数据。
query getPosts {
getPosts{
body
user {
username
}
}
}
我的问题是如何从客户端访问数据。
感谢您的帮助。
【问题讨论】:
标签: javascript mongodb mongoose graphql apollo