Home > OS >  How to access data from firestore write it into a file during Nuxt build?
How to access data from firestore write it into a file during Nuxt build?

Time:10-17

I'm trying to do this in the head from nuxt.config.js: and create json file in the folder lang

import fs from 'fs'
import { db } from '../GUILLERMO-QUINTERO-ROJAS/services/firebase'
db.collection('english').doc('english').get().then((response) => {
  fs.writeFile('lang/todos_1.json', JSON.stringify(response.data(), null, 2), 'utf-8', (err) => {
    if (err) return console.log('An error happened', err)
    console.log('File fetched from {JSON} Placeholder and written locally!')
  })
})

I followed this example who works perfectly with axios request https://stackoverflow.com/a/67689890/16899587

It's showing me this erros:

visualcode

browser

Or when I tried with this:

wrote own module

CodePudding user response:

I wrote this code inside of the nuxtconfig.js and this worked But I'm not sure is the best place and the best way to do, maybe you can give another ideas

hooks: {
  build: {
    done(builder) {
      builder.nuxt.options.buildDir
      const account = {
        email: process.env.NUXT_ENV_FIREBASE_EMAIL,
        password: process.env.NUXT_ENV_FIREBASE_PASSWORD
      }
      auth.signInWithEmailAndPassword(account.email, account.password).then(() => {
        db.collection('english').doc('english').get().then((response) => {
          fs.writeFile('lang/prueba.json', JSON.stringify(response.data(), null, 2), 'utf-8', (err) => {
            if (err) return console.log('An error happened', err)
            console.log('File fetched from {JSON} Placeholder and written locally!')
          })
        })
      })
      .catch((error) => {
        console.log(error)
      })
    }
  }
}

JSON file in the lang folder

json file in the lang folder

CodePudding user response:

OP needed to login first before using the rest of firebase's functions.

  • Related