尝试连接到 prismic 得到 "getFirst is not a function"
Trying to connect to prismic got "getFirst is not a function"
所以我运行正在使用 nodejs + express 开发一个应用程序,并尝试连接到 prismic API。文档使用的是 ESM,但我想使用 CommonJS 来完成,当我完成配置并尝试 运行 应用程序时,我收到错误 getFirst is not a function,我怀疑它可能在我将导出客户端声明为“module.export.client”的方式。但是因为我对这个不熟悉所以不能100%确定。
所以目前我的 app.js 文件看起来像这样
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const port = 3000
const client = require('./config/prismicConfig.js')
//Prismic
const prismic = require('@prismicio/client')
const prismicH = require('@prismicio/helpers')
const fetch = require('node-fetch')
const repoName = 'my-repo-name'
const accessToken = process.env.PRISMIC_ACCESS_TOKEN
const endpoint = prismic.getEndpoint(repoName)
const routes = [
{
type:'page',
path:'/'
}
]
module.exports.client = prismic.createClient(endpoint, {
fetch,
accessToken,
routes,
})
//Prismic
// template engine
app.set('views', path.join(__dirname,'views'))
app.set('view engine', 'pug')
//Middleware
app.use((req, res, next) => {
res.locals.ctx = {
prismicH,
}
next()
})
//end
app.get('/', async (req, res) => {
const document = await client.getFirst()
res.render('page', { document })
})
app.listen(port, ()=>{
console.log(` Example listen to http://localhost:${port}`)
})
这是我的 prismicConfig.js
require('dotenv').config()
console.log(process.env.PRISMIC_ENDPOINT, process.env.PRISMIC_CLIENT_ID)
const fetch = require ('node-fetch')
const prismic = require ('@prismicio/client')
const repoName = 'my-repo-name'
const accessToken = process.env.PRISMIC_ACCESS_TOKEN
const endpoint = prismic.getEndpoint(repoName)
const routes = [
{
type: 'page',
path: '/',
},
]
module.exports.client = prismic.createClient(endpoint, {
fetch,
accessToken,
routes,
})
因此,我将不胜感激对此的建议。预先感谢您阅读我:)
然后是client.client
,所以你应该这样要求它:
const {client} = require('./config/prismicConfig.js')
此外,您不需要在 server/app.js 中设置 prismic,因为它已经在配置中了:
尝试像这样更改 app.js:
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const port = 3000
const prismicH = require('@prismicio/helpers')
const {client} = require('./config/prismicConfig.js')
// template engine
app.set('views', path.join(__dirname,'views'))
app.set('view engine', 'pug')
//Middleware
app.use((req, res, next) => {
res.locals.ctx = {
prismicH,
}
next()
})
//end
app.get('/', async (req, res) => {
const document = await client.getFirst()
res.render('page', { document })
})
app.listen(port, ()=>{
console.log(` Example listen to http://localhost:${port}`)
})
如您在 post 中所述,您使用的不是默认导出,而是 prismicConfig.js 中的命名导出 module.exports.client
。
这意味着这个模块的使用者,app.js,需要解构 client
属性 :
const { client } = require('./config/prismicConfig')
所以我运行正在使用 nodejs + express 开发一个应用程序,并尝试连接到 prismic API。文档使用的是 ESM,但我想使用 CommonJS 来完成,当我完成配置并尝试 运行 应用程序时,我收到错误 getFirst is not a function,我怀疑它可能在我将导出客户端声明为“module.export.client”的方式。但是因为我对这个不熟悉所以不能100%确定。
所以目前我的 app.js 文件看起来像这样
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const port = 3000
const client = require('./config/prismicConfig.js')
//Prismic
const prismic = require('@prismicio/client')
const prismicH = require('@prismicio/helpers')
const fetch = require('node-fetch')
const repoName = 'my-repo-name'
const accessToken = process.env.PRISMIC_ACCESS_TOKEN
const endpoint = prismic.getEndpoint(repoName)
const routes = [
{
type:'page',
path:'/'
}
]
module.exports.client = prismic.createClient(endpoint, {
fetch,
accessToken,
routes,
})
//Prismic
// template engine
app.set('views', path.join(__dirname,'views'))
app.set('view engine', 'pug')
//Middleware
app.use((req, res, next) => {
res.locals.ctx = {
prismicH,
}
next()
})
//end
app.get('/', async (req, res) => {
const document = await client.getFirst()
res.render('page', { document })
})
app.listen(port, ()=>{
console.log(` Example listen to http://localhost:${port}`)
})
这是我的 prismicConfig.js
require('dotenv').config()
console.log(process.env.PRISMIC_ENDPOINT, process.env.PRISMIC_CLIENT_ID)
const fetch = require ('node-fetch')
const prismic = require ('@prismicio/client')
const repoName = 'my-repo-name'
const accessToken = process.env.PRISMIC_ACCESS_TOKEN
const endpoint = prismic.getEndpoint(repoName)
const routes = [
{
type: 'page',
path: '/',
},
]
module.exports.client = prismic.createClient(endpoint, {
fetch,
accessToken,
routes,
})
因此,我将不胜感激对此的建议。预先感谢您阅读我:)
然后是client.client
,所以你应该这样要求它:
const {client} = require('./config/prismicConfig.js')
此外,您不需要在 server/app.js 中设置 prismic,因为它已经在配置中了:
尝试像这样更改 app.js:
require('dotenv').config()
const express = require('express')
const app = express()
const path = require('path')
const port = 3000
const prismicH = require('@prismicio/helpers')
const {client} = require('./config/prismicConfig.js')
// template engine
app.set('views', path.join(__dirname,'views'))
app.set('view engine', 'pug')
//Middleware
app.use((req, res, next) => {
res.locals.ctx = {
prismicH,
}
next()
})
//end
app.get('/', async (req, res) => {
const document = await client.getFirst()
res.render('page', { document })
})
app.listen(port, ()=>{
console.log(` Example listen to http://localhost:${port}`)
})
如您在 post 中所述,您使用的不是默认导出,而是 prismicConfig.js 中的命名导出 module.exports.client
。
这意味着这个模块的使用者,app.js,需要解构 client
属性 :
const { client } = require('./config/prismicConfig')