![](/img/trans.png)
[英]Returns object / JSON value instead of full value, React, Graphql, Nextjs
[英]GraphQL response returns [Object] instead of data
GraphQL 新手并使用它从 Wordpress API 返回数据。 在 GraphiQL Wordpress IDE 中创建查询时,它返回正确的数据,但是当我在 NEXTJS 应用程序中实现查询并执行console.log
时,我看到[Object]
而不是响应中的数据。 我很困惑为什么在使用 IDE 创建查询时它会正确显示,但在实现时不会按预期呈现数据。
我的设置:
//apollo-client.js
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: `${process.env.NEXT_PUBLIC_GRAPH_QL}`,
cache: new InMemoryCache({
addTypename: false,
}),
});
export default client;
//index.ts
import { gql } from '@apollo/client';
import client from '../../apollo-client';
export async function getStaticProps() {
const { data } = await client.query({
query: gql`
query getPosts {
posts(first: 20) {
nodes {
title
featuredImage {
node {
sourceUrl
}
}
}
}
}
`,
});
return {
props: {
posts: data
},
};
console.log(posts)
:
{
posts: {
nodes: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object]
]
}
}
如果您真的想查看 console.log 中存在的实际数据是什么,您可以使用
console.log(JSON.stringify(posts)):
您还可以通过使用访问特定对象,
console.log(data.posts.nodes):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.