![](/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.