M
M
mayday692020-08-20 14:11:19
JavaScript
mayday69, 2020-08-20 14:11:19

How to pass array from server node js file to client simple js file with scripts?

There is a server node js file in which the database is processed and transfers all the information to the object. Like this:
index.js file

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;
}

When trying to pass an object through module.exports on the client side, the object is not visible
here the export occurs:
index.js file
app.get('/orderlist', checkUserSession, async function(request, response) {
    returnOrderArray().catch(error => console.log(error)).then(() => console.log(order), module.exports.order = order,
        response.render("orderlist.ejs", { username: request.session.username }));
});


and here import :
file orderlist.js
var ind = require('../../index')


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


just in case file location:

5f3e5a243732e477229366.png

What could be the problem and what is the best way to pass the order object to another js?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
echo_vladik, 2020-08-21
@echo_vladik

I don’t know much, but it seems that through socket.io you can transfer objects to the client, and dynamically through event subscriptions from the server to the client and vice versa, and I would call the function getOrders(filter={bububu, bububu}) for example

T
t800zippygod, 2020-08-21
@t800zippygod

Wait, are you exporting from a server script to a client script??? I'm afraid that won't work.
Data between the server and the client must be transferred via HTTP transport

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question