I know this has been asked many times, but I’ve tried everything I can find with no success. Thank you for any patience as I am not a professional programmer. I simply like to automate my tasks when working in a CMS. The specific use case is to transform one row in a CSV file into a page in the CMS using the CMS’s REST API. Is anyone able to guide me in running this script, especially its functions in a loop, in order?
The parent function (main
) is async
because the library used to transform TSV to JSON (csvtojson
) requires await
. I also used await
when calling the jq
library to manipulate the JSON after using csvtojson.
The main issue lies with the two API call functions in the loop. In the iteration of the code below, the console showed that the second function ran 5 times, failing each time because it lacked the data piped to it by the first function/API call. Then the second function ran 6 times before I stopped execution.
Code:
var csv = require("csvtojson");
var jq = require('node-jq');
var parentFolder = 1234
var token = ""
var csvFilePath = 'licenses.tsv'
var parentPublicationId = 5678
async function main() {
// Transform CSV/TSV to JSON
json = '';
await csv({
delimiter: 't'
})
.fromFile(csvFilePath)
.then((jsonObj) => {
json = jsonObj
})
// Create an array of objects with certain keys
jsonOutput = '';
await jq.run('[.[] | {name: .sourceName}]', json, { input: 'json' })
.then((output) => {
jsonOutput = output
})
.catch((err) => {
console.error(err)
})
parsed = JSON.parse(jsonOutput)
// Create a page in the CMS for each CSV/TSV row
for (var obj in parsed) {
id = '';
async function createDocument() {
var name = parsed[obj].name;
var content = `<title>${name}</title></section>`;
var inputBody = {
"parent": parentFolder,
"name": name,
"content": content,
"subtype": "component"
};
var headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic ' + token,
};
fetch('https://thesite.com/api/v2/documents',
{
method: 'POST',
body: JSON.stringify(inputBody),
headers: headers
})
.then(function (res) {
return res.json();
}).then(function (body) {
console.log(body);
id = body.id;
// Capture document ID from response
console.log(id);
});
return
}
// Add the new page to an existing table of contents
async function updatePublication() {
await createDocument();
var inputBody = {
"parent": parentPublicationId,
"document": id,
"position": "null",
};
var headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic ' + token,
};
fetch('https://thesite.com/api/v2/forks',
{
method: 'POST',
headers: headers,
body: JSON.stringify(inputBody)
})
.then(function (res) {
return res.json();
}).then(function (body) {
console.log(body);
});
}
updatePublication();
}
}
main()
If you would like me to provide more examples of what I’ve tried I am happy to – basically different iterations of async/await, promises and then
, return
, callbacks, and more. Over and over, things always run out of order. Thank you for any kind help.