Home > Software design >  Is there any chance to output map method into variable as an array of objects?
Is there any chance to output map method into variable as an array of objects?

Time:12-18

Hello All!

I want to store users in folder as a file where file name is equal to user_id.

data
| - users
      | - afdcab7e-b595-4a15-be0f-5f0337bd1317.json
      | - fdfacb7i-bk00-4a15-be0f-5f0337b1d991.json

Each user has their own data for example

{
    "_id": "afdcab7e-b595-4a15-be0f-5f0337bd1317",
    "email": "[email protected]",
    "password": "$2a$12$nIoudV7eXmJbU7e/P6YCbOccUkTbp8tcQKhyCEfmNOLihrW6QqPTC"
}
{
    "_id": "fdfacb7i-bk00-4a15-be0f-5f0337b1d991",
    "email": "[email protected]",
    "password": "$2a$12$nIoudV7eXmJbU7e/P6YCbOccUkTbp8tcQKhyCEfmNOLihrW6QqPTC"
}

Then I want to read the contents of all files and put the objects into one temporary array.

exports.indexSignin = (req, res) => {

    fs.readdir('./data/users', (err, files) => {
        if (err) console.log(err);

        const obj = [];
        files.map((file) => {
            fs.readFile(`./data/users/${file}`, 'utf-8', (err, data) => {
                if (err) console.log(err);
                obj.push(JSON.parse(data))
                console.log(obj)
            });
        });
        console.log(obj) //There obj is empty but I want an array
    });

    res.render('./index/index');
});

As an output I want to have an array saved into variable like this listed below:

[
{
    "_id": "afdcab7e-b595-4a15-be0f-5f0337bd1317",
    "email": "[email protected]",
    "password": "$2a$12$nIoudV7eXmJbU7e/P6YCbOccUkTbp8tcQKhyCEfmNOLihrW6QqPTC"
},
{
    "_id": "fdfacb7i-bk00-4a15-be0f-5f0337b1d991",
    "email": "[email protected]",
    "password": "$2a$12$nIoudV7eXmJbU7e/P6YCbOccUkTbp8tcQKhyCEfmNOLihrW6QqPTC"
}
]

Do you have any ideas how to use mapped data externally or refactor it into a better way?

CodePudding user response:

This is the same code of mine that works. I hope that helps you.

const { readdir, readFile } = require("fs/promises");

const readFiles = async () => {
    try {
        const path = "./test"
        const files = await readdir(path);
        console.log(files)

        const fileAwaits = files.map(file => readFile(`${path}/${file}`, "utf8"))
        const contents = await Promise.all(fileAwaits)
        console.log(contents.map(co => JSON.parse(co)))
    } catch (err) {
        console.error(err)
    }
}

readFiles()

So if you want to use this inside your API handlers change it as bellow:

exports.indexSignin = async (req, res) => {

    try {
        const path = "./test" // replace path by your own
        const files = await readdir(path);
        console.log(files)

        const fileAwaits = files.map(file => readFile(`${path}/${file}`, "utf8"))
        const contents = await Promise.all(fileAwaits)
        const arrayContent = contents.map(co => JSON.parse(co))
        console.log(arrayContent);
    } catch (err) {
        console.error(err)
    }
    res.render('./index/index');
});

CodePudding user response:

Thank You guys! I solved my problem with Your help.

Here is a working example which I needed:

exports.indexSignin = (req, res) => {

    const readFiles = async () => {
        try {
            const path = "./data/users"
            const files = await readdir(path);
    
            const fileAwaits = files.map(file => readFile(`${path}/${file}`, "utf8"))
            const contents = await Promise.all(fileAwaits)
            return contents.map(co => JSON.parse(co))
        } catch (err) {
            throw err;
        }
    }

    readFiles()
        .then(test => console.log(test))
        .catch(err => console.log('Directory not found.'))
        .finally(() => console.log('Rest of the code...'));

    res.render('./index/index');


    // or IIFY which do the same 
    (async () => {
        try {
            const test = await readFiles();
            console.log(test);
        } catch (err) {
            console.log('Directory not found.');
        }
        console.log('Rest of the code...')
        res.render('./index/index');
    })()
};
  • Related