I use a ‘dato.config.js’ file in my project to create JSON files from the CMS data. The content model is called ‘projects’ and in there I have a Modular Content Field called ‘body’.
When running the ‘dato dump’ command I get the following error in the terminal (“Cannot read property ‘nl’ of null”) which seems to be related to the locales.
The config does work for all the other fields (in 2 languages), except for any of the Modular Content Fields. I used the suggested code from this page: https://www.datocms.com/docs/static-generators/other-ssg/modular-content but I can’t get it to output the data from ‘body’ inside my JSON files.
Do you have any suggestions? Here’s an example of my code:
module.exports = (dato, root, i18n) => {
// PROJECTS
// iterate over all the administrative area languages
i18n.availableLocales.forEach((locale) => {
// create a separate folder for each locale
root.directory(`cms/content/projects-${locale}`, (projectsFolder) => {
// switch to the nth locale
i18n.withLocale(locale, () => {
// iterate over the 'Projects' records
dato.projects.forEach((project) => {
// create a localized JSON file for each project
projectsFolder.createPost(`${project.slug}_${locale}.json`, 'json', {
frontmatter: {
layout: 'project',
languages: [`${locale}`],
xid: project.id,
slug: project.slug,
heroImage: project.heroImage.toMap(),
title: project.title,
body: project.body.toMap(), // Modular Content Field, not working
subtitle: project.subtitle,
organisation: project.organisation,
relatedTitle: project.relatedTitle,
ctaTitle: project.ctaTitle
}
})
})
})
})
})
}
Thank you for your reply. I tried again today, but the error was still the same. Should I change something in my dato.config.js file after your update?
Is it also possible to get a full JSON object with all records in one go, without specifying each data field inside frontmatter?
Example: I can log ‘dato.projects’ inside my terminal to see all the projects, but I don’t know how to save this in a single JSON file using dato.config.js.
@mat_jack1 I gave this a try, but it’s not exactly the solution I am looking for. It misses some of the data which I need to have in order to build a static website (with Nuxt / Vue components).
The best solution would be to have a single JSON file with all the projects inside it, which I can use in a Vue component. I would like to collect all this ‘projects’ data and let the Vue component decide how to use this.
It seems that this data is available (dato.projects for example), but is there a way to save this in a file?
I found a solution with node-fs to concat all JSON files inside a folder into one JSON file. I need this file in Vue to loop over the projects in the JSON and create dynamic routing for each project page.
In this image you can see the result. Example: It says “body:” > “en:” but how can I also get the content from the ID fields (556579, 556580 etc.) inside the same JSON file?
@pims in the same dump you should find an object with the IDs specified in the list. You should match the ID with the object yourself, as you could have multiple instances of the same object around we reference via IDs.