1

I have multiple json5 files that I need to join. Like in MySQL but with nuxt-content for a static stite with no SSR. I have done something but it doesn't work when I do nuxt generate

Ex:

data.json5
{ 
  title: 'title'
  date: date,
  cars: ['honda-2019', 'audi-2020'] // array of car slug
}

honda-2019.json5
{
  title: 'Honda 2019'
  year: '2019',
  parts: ['motor', 'wheels']
}

motor.json5
{
  title: 'motor'
  working: true
}

Final object

{ 
  title: 'title'
  date: date,
  cars: [
   {
      title: 'Honda 2019'
      year: '2019',
      parts: [{...}, {...}]
    },
    { ... }
  ]
}
Derian André
  • 220
  • 3
  • 12
  • You can use `deep` as shown here: https://stackoverflow.com/a/69002483/8816585 Then it comes down to vanilla JS find and merge objects. What did you tried so far? – kissu Sep 20 '21 at 06:47
  • Hi kissu, I was meaning something different with `deep` this is my repo: https://github.com/Equreka/Equreka I am doing it right now with multiple functions (probably not the best aproach) in `utils/data.js` everything works great in dev but not when I try nuxt generate and start. It's a webapp for equations, formulas, constants, etc. and I have everything organized in `content/`, you can check out for ex. `/content/equations/mass-energy-equivalence.json5`. – Derian André Sep 20 '21 at 07:01
  • What's the difference? Do you see an error? – kissu Sep 20 '21 at 07:04
  • Seems like promises are not getting solved or something: `TypeError: Cannot read properties of undefined (reading '0')` also search dont work `Uncaught (in promise) Error: // not found` – Derian André Sep 20 '21 at 07:06
  • Where is this happening? Can you update your code with the related snippet? – kissu Sep 20 '21 at 07:09
  • Everything it's updated now. Error is in `/pages/_type/_slug.vue` the function is `const data = await getData($content, params, error)` that is in the file `/utils/data.js` – Derian André Sep 20 '21 at 07:19
  • I'm not sure but this [link](https://stackoverflow.com/a/52642650/11530419) can help you to solve it. – ElsaKarami Sep 20 '21 at 07:26
  • Thanks for your answer. I think I dind't explain myself well, `nuxt generate` works in the sense that it generate the files but it doesn't works as expected, also you can try a live version here: https://preview.equreka.com/ you can go to physics -> mass-energy equivalence and open dev console. – Derian André Sep 20 '21 at 07:34
  • Does your default export work in `data.js`? Do you have your data there? Did you tried plugging it to an event on a button click? – kissu Sep 20 '21 at 10:12
  • `data.js` it's just a collection of functions with loops to get all nested data. Haven't tried with a event on a button as I need the final object in asyncData before the page is loaded. It seems that i'm getting a Promise object (in the dist static build) instead of the data. but why does it works in dev but not in dist?. – Derian André Sep 20 '21 at 10:27
  • I found out that `.content('/', { deep: true }).search()` wont work but this will `.content({ deep: true }).search()`. – Derian André Sep 21 '21 at 05:14
  • I also don't know what I did exactly, but I got it working again. I removed all `.only()` and `.without()`, anyway would be nice to have this working with automaticlly with nuxt-content `nestedProperties`, or maybe passing it to `$content(path, options, nested)` or as another function `.nest(['car.slug'])` or something like that. I'll post it in the github page. – Derian André Sep 21 '21 at 05:19

0 Answers0