1

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

kissu
  • 40,416
  • 14
  • 65
  • 133
Santiago
  • 31
  • 5
  • Hi, do what is not working there? Any errors/issues? Does it work in a regular `.vue` file? – kissu Oct 15 '22 at 22:42
  • Nothing, not erros, it's not showing me something weird. I tried to do in another vue file but I can't `import fs from 'fs'` `This dependency was not found:` I tried with `npm install --save fs` But It's not working, any suggestions ? – Santiago Oct 16 '22 at 16:48
  • Now I added the pictures with the errors – Santiago Oct 16 '22 at 17:02
  • wrong place to put code, instead make a build module https://go.nuxtjs.dev/config-modules i.e `module.exports = function writeLang(options) { this.nuxt.hook('build:done', (builder) => {// your code here})}` – Lawrence Cherone Oct 16 '22 at 17:09
  • when I tried with this example works perfect: https://stackoverflow.com/a/67689890/16899587 but when I used my frirebase request didn't work – Santiago Oct 16 '22 at 17:55
  • What kind of error do you have? – kissu Oct 16 '22 at 19:54
  • I added the pictures in the question, I think I'm having problems with the firebase request, it's not working here in the nuxtconfig.js... the two pic of that are in the question "It's showing me this erros:" "missing or insuficient permisions" and `err_incomplete_chunked_encoding` – Santiago Oct 16 '22 at 21:26
  • `missing or insuficient permisions` is the one. The rest is probably unrelated IMO. – kissu Oct 16 '22 at 21:43
  • Mmm it's like I can't use the firebase request in the nuxtconfig, mabe I can try in a vue file. But always said when I tried to import the `fs` this: `This dependency was not found:` I can't use `fs.writeFile()` inside of a funtion in a vue file ? – Santiago Oct 16 '22 at 21:50
  • Okay I got it, thanks guys for the time, my problem was with the auth with firebase. So I log-in then I did the rest – Santiago Oct 16 '22 at 22:47

2 Answers2

2

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

kissu
  • 40,416
  • 14
  • 65
  • 133
Santiago
  • 31
  • 5
  • Looks nice to me. – kissu Oct 16 '22 at 23:01
  • this function will only be executed once? @kissu I mean if I finished my app and I hosted, this function is not going to update the new data to the json file – Santiago Oct 18 '22 at 12:21
  • 1
    You can check the [details here](https://nuxtjs.org/docs/configuration-glossary/configuration-hooks#list-of-hooks), it depends of your configuration I'd say. But yeah, as you can guess you need to rebuild your app if using SSG to get the info updated. – kissu Oct 18 '22 at 12:26
0

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

kissu
  • 40,416
  • 14
  • 65
  • 133