Click here to Skip to main content
15,946,988 members
Home / Discussions / JavaScript
   

JavaScript

 
QuestionI am using javascript to convert a large table to a pdf Pin
Zahira CHOUIBA26-Jan-23 3:22
Zahira CHOUIBA26-Jan-23 3:22 
QuestionRe: I am using javascript to convert a large table to a pdf Pin
Jeremy Falcon26-Jan-23 8:02
professionalJeremy Falcon26-Jan-23 8:02 
AnswerRe: I am using javascript to convert a large table to a pdf Pin
RickyJudith8-Feb-23 0:24
RickyJudith8-Feb-23 0:24 
QuestionI have problem importing images of Json File on HTML Table Pin
Member 1590151922-Jan-23 2:14
Member 1590151922-Jan-23 2:14 
AnswerRe: I have problem importing images of Json File on HTML Table Pin
Jeremy Falcon24-Jan-23 4:30
professionalJeremy Falcon24-Jan-23 4:30 
Questionsoftware access control Pin
Member 1589900419-Jan-23 0:51
Member 1589900419-Jan-23 0:51 
AnswerRe: software access control Pin
Richard Deeming19-Jan-23 1:25
mveRichard Deeming19-Jan-23 1:25 
QuestionUse async function to retrieve data while looping through from another function to insert objects Pin
Member 1589206711-Jan-23 23:35
Member 1589206711-Jan-23 23:35 
Hello! I want to say that I just started working with nodejs. I used before php/laravel.

I have 2 tables, one is for posts one is for posts_actions. Every user can create a post and other users can like/comment on that post. Every action on that post (eg: like) will be stored in table posts_actions.

I'm trying to load the posts from database, loop through them to add an extra object with all the data from table posts_actions.

But at the end, when I send back the data to frontend, the data is empty, why is that and how can I solve this?


app.post("/api/loadposts", function(req, res) {
    let posts = [];
    let posts_actions = [];
    const userid = req.body.userID;

    db.query("SELECT * FROM posts ORDER BY id DESC",
    (error, result) => {
        if(error) {
            return res.status(200).send({ error: true });
        }
       
        posts = result;
    });

    for(let p; p < posts.length; p++)
    {
        db.query("SELECT * FROM posts_actions WHERE post_id = ? AND user_id = ? LIMIT 1", [posts[p].id, userid],
            (e, r) => {
                if(e) {
                    posts[p].action = [];
                }
                else if(r.length > 0) {
                    posts[p].action = r[0];
                }
                else {
                    posts[p].action = [];
                }
        });
    }

    res.status(200).send({ data: posts });
});


I know that I must use async/await/promise, I followed some topics and tried the code below, but the data I send to frontend is empty.
A little help here, what can I do to have the result I want?


async function doQuery(query, params = []) {
    function queryData(query, params) {
        return new Promise(function(resolve, reject) {
            db.query(query, params, function (err, rows, fields) {
                if (err) {
                    return reject(err);
                }
                resolve(rows);
            });
        });
    }

    queryData(query, params).then(function(v) {
        return v;
    }).catch(function(v) {
        return [];
    })

    return null;
}

async function getAllPosts(userid)
{
    try {
        let posts = await doQuery("SELECT * FROM posts ORDER BY id DESC");

        for(let p = 0; p < posts.length; p++)
        {
            let actions = await doQuery("SELECT * FROM posts_actions WHERE post_id = ? AND user_id = ? LIMIT 1", [posts[p].id, userid]);

            if(actions.length)
            {
                posts[p].actions = actions[0];
            }
            else
            {
                posts[p].actions = [];
            }
        }

        return posts;
    } catch (error) {
        return error;
    }
}

app.post("/api/loadposts", async function(req, res)
{
    let posts = await getAllPosts(req.body.userID);
    res.send({ data: posts });
});




modified 12-Jan-23 6:11am.

AnswerRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Richard Deeming12-Jan-23 0:02
mveRichard Deeming12-Jan-23 0:02 
QuestionRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Member 1589206712-Jan-23 1:38
Member 1589206712-Jan-23 1:38 
AnswerRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Richard Deeming12-Jan-23 2:31
mveRichard Deeming12-Jan-23 2:31 
PraiseRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Member 1589206712-Jan-23 3:17
Member 1589206712-Jan-23 3:17 
GeneralRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Richard Deeming12-Jan-23 3:39
mveRichard Deeming12-Jan-23 3:39 
AnswerRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Jeremy Falcon12-Jan-23 8:44
professionalJeremy Falcon12-Jan-23 8:44 
AnswerRe: Use async function to retrieve data while looping through from another function to insert objects Pin
Leon Scott Kennedy13-Jan-23 5:25
Leon Scott Kennedy13-Jan-23 5:25 
QuestionJava Script Pin
Prerna Kharbanda3-Jan-23 20:56
professionalPrerna Kharbanda3-Jan-23 20:56 
AnswerRe: Java Script Pin
Richard MacCutchan3-Jan-23 21:33
mveRichard MacCutchan3-Jan-23 21:33 
Questioncalendar validation Pin
Jithin P 202227-Dec-22 20:45
Jithin P 202227-Dec-22 20:45 
AnswerRe: calendar validation Pin
Dave Kreskowiak28-Dec-22 3:59
mveDave Kreskowiak28-Dec-22 3:59 
AnswerRe: calendar validation Pin
Jithin P 202229-Dec-22 3:31
Jithin P 202229-Dec-22 3:31 
AnswerRe: calendar validation Pin
Jeremy Falcon5-Jan-23 11:13
professionalJeremy Falcon5-Jan-23 11:13 
AnswerRe: calendar validation Pin
Jeremy Falcon5-Jan-23 11:17
professionalJeremy Falcon5-Jan-23 11:17 
Questionhow to modify cloned element draggable attribute ? Pin
VernonM23-Dec-22 10:37
VernonM23-Dec-22 10:37 
AnswerRe: how to modify cloned element draggable attribute ? Pin
VernonM23-Dec-22 14:57
VernonM23-Dec-22 14:57 
AnswerRe: how to modify cloned element draggable attribute ? Pin
VernonM24-Dec-22 2:43
VernonM24-Dec-22 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.