jeudi 20 août 2020

How to pass object from one file to another in javascript?

I have node js server file (index.js) and client file (orderlist.js) In index.js i am getting promise object , like that

function returnOrderArray() {
    var i = 0;
    const promise = new Promise((resolve, reject) => {
        connection.query('SELECT * FROM orders', function(error, results) {
            while (i < results.length) {

                order.id[i] = results[i].id;
                order.wavetype[i] = results[i].wavetype;
                order.color[i] = results[i].color;
                order.thick[i] = results[i].thick;
                order.readydate[i] = results[i].readydate;
                order.createdate[i] = results[i].createdate;
                order.manager[i] = results[i].manager;

                i++;
            }
            resolve(order);
            // console.log(order);


        });
    });
    return promise;
}

then i want to pass it to other js file. I tried to do that with module.exports

app.get('/orderlist', checkUserSession, async function(request, response) {
    returnOrderArray().catch(error => console.log(error)).then((() => {
        module.exports.order = order;
        response.render("orderlist.ejs", { username: request.session.username });
    })).catch(error => console.log(error));
});

and then import it in orderlist.js

var ind = require('../../index')


function asd() {
    alert(ind.order);
}

but it seems not to work.

What am i doing wrong , and what's the best way to pass objects to other files in js?

oh , and file architecture

filearch




Aucun commentaire:

Enregistrer un commentaire