Next.js Discord

Discord Forum

Help - Vercel failed to fetch in production

Unanswered
House Sparrow posted this in #help-forum
Open in Discord
House SparrowOP
Hello,

I am using google cloud database with my next js App router app.

To get some data from the database, it's done through the creation of a Datastore need to be pass as keyFileName a datastore-service-account.json.
It works fine in local. However, in production it seems impossible to get a correct path. I get this error:
Failed to fetch recipe: [Error: ENOENT: no such file or directory, open '/var/task/lib/datastore-service-account.json'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/var/task/lib/datastore-service-account.json'
}

Could you help me figure out how to access this .json file in production?

Thank you!

Tried multiples things in vain:

import { Datastore } from '@google-cloud/datastore'
import path from 'path'
import datastoreServiceAccount from '@/datastore-service-account.json'
// Determine the correct path for the service account file
const keyFilenamePath = process.env.GOOGLE_APPLICATION_CREDENTIALS
? process.env.GOOGLE_APPLICATION_CREDENTIALS
: path.join(__dirname, '..', 'datastore-service-account.json')



const datastore = new Datastore({
projectId: 'recipe-app-redwood',
keyFilename: keyFilenamePath
})

export default {
recipe: () => ({
all: async () => {
const query = datastore.createQuery('Analysed_Recipe')
try {
const [recipes] = await datastore.runQuery(query)
return recipes
} catch (error) {
console.error('Failed to fetch recipes:', error)
throw error
}
},
get: async (id) => {
const query = datastore
.createQuery('Analysed_Recipe')
.filter('id', '=', id)
try {
const [recipes] = await datastore.runQuery(query)
return recipes.length > 0 ? recipes[0] : null
} catch (error) {
console.error(Failed to fetch recipe with ID ${id}:, error)
throw error
}
}
})
}


thanks for the help!

0 Replies