I am destructing the data from the function
let { destructValue } = require("./destructValue.js");
const fs = require("fs");
const path = require("path");
function retrunValues(data) {
fs.readdir("./userData", (err, files) => {
if (err) throw console.log(err.message);
else {
files.forEach(async (file) => {
fs.readFile(`./userData/${file}`, (err, data) => {
destructValue(data);
let jsonObject = {};
if (destructValue(data).length !== 0) {
jsonObject = {
name: data.name,
value: [...destructValue(data)],
};
console.log(jsonObject);
}
});
});
}
});
}
so after doing console.log(jsonObject) I am getting this values
{
name: "Tomas",
value:[{
age: "21",
address: "New York"
}]
}
{
name: "Jerry",
value:[{
age: "22",
address: "Tokyo"
}]
}
this output look fine to me as this was the expected output so I tried to call jsonObject into new schema like this
let { destructValue } = require("./destructValue.js");
const fs = require("fs");
const path = require("path");
function retrunValues(data) {
fs.readdir("./userData", (err, files) => {
if (err) throw console.log(err.message);
else {
files.forEach(async (file) => {
fs.readFile(`./userData/${file}`, (err, data) => {
destructValue(data);
let jsonObject = {},
testingObject = {};
if (destructValue(data).length !== 0) {
jsonObject = {
name: data.name,
value: [...destructValue(data)],
};
}
testingObject = {
default: "default value",
got: jsonObject,
};
console.log(testingObject);
});
});
}
});
}
so after doing console.log(testingObject) I am getting this value
{ default: 'defaultObject'}
{ default: 'defaultObject'}
{ default: 'defaultObject',
name: "Tomas",
value:[{
age: "21",
address: "New York"
}]
}
{ default: 'defaultObject'}
{ default: 'defaultObject'}
{ default: 'defaultObject',
name: "Jerry",
value:[{
age: "22",
address: "Tokyo"
}]
}
As I was not expecting this output as my expected output was like this
[
{
"default": "defaultObject",
"got": [
{
"name": "Tomas",
"value": [{ "age": "21", "address": "New York" }]
},
{
"name": "Jerry",
"value": [{ "age": "22", "address": "Tokyo" }]
}
]
}
]
but I am not getting the expected value and I don't know where it went wrong how can I achieve my expected value
destructValue.js
const {age,address}=require(valueGot.js);
function destructValue(data){
const destructSchema=[];
for(const value of data){
switch(value.userDetails){
case "age":
destructSchema.push(age(data));
break;
case "address":
destructSchema.push(address(data));
break;
}
}
return destructSchema;
}
module.exports={destructValue}
from Real json value is more different then expected value using javascript / node js
No comments:
Post a Comment