hello ,
i wanna do something as below but the problem that names and address out of the connection query are empty how can i assign them and the send them to the page?

app.get('/reports', (req, res)=>{


    let sql = "SELECT DISTINCT name from users_table  ;";
    let names;
    let query = connection.query(sql,(err,results) => {
    if(err) throw err;
    names=results
});


let sql2 = "SELECT DISTINCT address from users_table  ;";
    let addresss ;
    let query2 = connection.query(sql2,(err,results) => {
    if(err) throw err;
    addresss=results
});


res.render('reports',{
    title:'Reports ',
    names:names,
    addresss:addresss,



    })

});

Recommended Answers

Please clarify what the question is. Your topic asks one thing but then you go in another direction in your post.

I'm reading https://www.geeksforgeeks.org/how-to-save-connection-result-in-a-variable-in-node-js/ which appears to answer the topic title's question.

If my query is empty I usually check my query …

Jump to Post

All 4 Replies

i wanna get the result of sql 1 and save it in names and the result of sql 2 and save it in address and then in render send the results in reports page but the problem out of sql 1 connection if i consoled the name i find it empty however in the connection there is result

so why it is empty out of the connection ??

Ahh, now I see it. Your post title asked about the connection result which as an old MySQL person I took as exactly that.
Nod to https://www.connectionstrings.com/mysql/

If I swap your use of connection for "query" then that changes the question to something else.

While I can't guess your end goal of the query I would change the query to a single query since both appear to be from the same table.

commented: assume that the two queries are from different tables what should i do now? +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.