【问题标题】:Trying to connect to prismic got "getFirst is not a function"尝试连接到 prismic 得到“getFirst 不是函数”
【发布时间】:2022-02-01 19:35:32
【问题描述】:

所以我正在使用 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,
})

因此,我将不胜感激这方面的建议。提前感谢您阅读我:)

【问题讨论】:

    标签: node.js express prismic.io


    【解决方案1】:

    那就是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}`)
    })
    

    【讨论】:

    • 你好@traynor 谢谢你,谢谢你,非常感谢:) 花时间查看整个代码,这非常有帮助,我实际上也在问我同样的问题,因为在官方doc 出现了两次,这更有意义。
    【解决方案2】:

    正如您在帖子中所说,您没有使用默认导出,而是在 prismicConfig.js 中使用命名导出 module.exports.client

    这意味着这个模块的使用者 app.js 需要解构client 属性:

    const { client } = require('./config/prismicConfig')
    

    【讨论】:

      猜你喜欢
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2020-01-16
      • 2017-12-19
      • 2013-03-28
      • 1970-01-01
      • 2015-12-30
      相关资源
      最近更新 更多