1

So I'm running an app with nodejs + express, and trying to connect to the prismic API. keep getting " '[Link resolver error] Unknown type\n' ", I understand from the message it's something about my routes but I'm unsure how to fix it

prismic config

require('dotenv').config()
const fetch = require('node-fetch')
const prismic = require('@prismicio/client')

const repoName = process.env.PRISMIC_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
})

app.js

require('dotenv').config()
const path = require('path')
const express = require('express')
const prismicH = require('@prismicio/helpers')
const { client } = require('./config/prismicConfig.js')

const app = express()
const port = process.env.PORT || 3000

// 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()
})

app.get('/', async (req, res) => {
  const document = await client.getFirst()
  res.render('page', { document })
})

app.get('/about', (req, res) => {
  res.render('pages/about')
})
app.get('/collections', (req, res) => {
  res.render('pages/collections')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

prismic documents prismic documents

folder structure

folder structure

Edit: i fixed the issue the issue was I was trying to render page, instead of pages/home

app.get('/', async (req, res) => {
  const document = await client.getFirst()
  res.render('page', { document })
})

so i just edited the res.render to :

res.render('pages/home', { document })
Taste of Leaving
  • 304
  • 2
  • 20

2 Answers2

0

The error is likely being thrown because the routes object is not structured correctly. It should be structured like this:

const routes = {
  page: '/',
  blog_post: '/blog/:uid'
}

module.exports.client = prismic.createClient(endpoint, {
  fetch,
  accessToken,
  routes
})
  • unfortunately this not fixed the issue, it gets new error type: 'Link resolver error', message: `'type' is undefined on object: {"page":"/","blog_post":"/blog/:uid"}` – Taste of Leaving Nov 29 '22 at 13:58
0

I fixed the error,

app.get('/', async (req, res) => {
  const document = await client.getFirst()
  res.render('page', { document })
})

the mistake was the I'm trying to render page, instead of pages/home so i edited this line

res.render('pages/home', { document })
Taste of Leaving
  • 304
  • 2
  • 20