如何从Prismic (CMS)拨打next.js application API?在next.js我之前有过:
import React from 'react'
import Link from 'next/link'
import 'isomorphic-fetch'
export default class Index extends React.Component {
static async getInitialProps () {
const res = await fetch('https://myAPI_URL')
const json = await res.json()
return { cars: json.results }
}
render () {
return (
<div>
<p>I can see the data {this.props.cars} </p>
</div>
)
}
}
但我想使用Prismic开发工具包作为依赖项,如in their documentation.
所述npm install prismic-javascript prismic-dom --save
调用Prismic的代码:
const Prismic = require('prismic-javascript');
const apiEndpoint = "https://your-repository-name.prismic.io/api/v2";
const apiToken = "this_is_a_fake_token_M1NrQUFDWUE0RYvQnvv70";
Prismic.getApi(apiEndpoint, {accessToken: apiToken}).then(function(api) {
return api.query(""); // An empty query will return all the documents
}).then(function(response) {
console.log("Documents: ", response.results);
}, function(err) {
console.log("Something went wrong: ", err);
});
但是如何在getInitialProps中使用next.js进行Prismic API调用?例如:
import React from 'react'
import Link from 'next/link'
import 'isomorphic-fetch'
export default class Index extends React.Component {
static async getInitialProps () {
// fetch Prismic API and return the results
}
render () {
return (
<div>
<p>Showing data fetched from API here</p>
</div>
)
}
}
答案 0 :(得分:3)
您已经准备好了所有组件。而不是=IFERROR(VLOOKUP(OFFSET(Sheet1!$A$1,ROW()-1,0),Employees!A$2:B$100,2,false),"")
,等待fetch
请求。然后返回结果。
Prismic