I want to read the values of key
, access
, path
, bucket
and bucketPath
and use them in the JSON file test.json
.
I have a function that reads the content of configuration.js
and attempts to write to test.json
. Currently, I am able to write the values of bucket
.I get the changed/new values and lines of null
for the rest of the json.
I want to always return the new values and the other objects in the file. Also, in cases where bucket
already has a value, I want it replaced by whatever is read from configuration.json
How can I fix this, and how can i change the values for the rest access
, key
, path
and bucketpath
?
index.js
const fs = require("fs").promises;
async function readJSON(filePath, values) {
const data = await fs.readFile(filePath);
try {
return JSON.parse(data);
} catch (err) {
console.log(err);
}
}
(async() => {
const credentials = await readJSON("./configuration.json");
const path = credentials.path;
const bucket = credentials.bucket;
const access = credentials.access;
const key = credentials.key;
const bucketPath = credentials.bucketPath;
const data = await jsonReader("./test.json");
const finalJSON = data.data ? .map((x) => {
if (x.type == "s3 credentials") return { ...x, bucket };
});
await fs.writeFile(
"./test.json",
JSON.stringify({
data: finalJSON
})
);
})();
test.json
{
"label": "storage record",
"data": [{
"id": "8902uw",
"type": "config",
"values": {
"access": "$access",
"key": "$key"
}
},
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
"id": "90yueps",
"type": "upload",
"input": "localhost: `$path`"
},
{
"id": "9028901",
"type": "change",
"name": "Adjust data",
"measure": [{
"t": "setter"
},
{
"p": "filename",
"to": "$join(["$bucketPath", data])"
}
],
"fixed": ""
}
]
}
configuration.json
{
"key": "880082",
"access": "793082",
"path": "/store",
"bucket": "testBucket",
"bucketPath": "/record"
}
Currently, when I run this, I get:
{
null,
"data": [{
null,
null,
null,
null
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
null,
null,
null
]
}
2
Answers
[Updated answer]
From what you comment:
[Original answer]
There is a problem with the function you pass to the
map
function.The condition
if
withoutelse
.I think you need
else { return x; }
to return original data ifx.type
is not what you expected.might this be a solution !