Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
M

Member_15892067

@Member_15892067
About
Posts
3
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Use async function to retrieve data while looping through from another function to insert objects
    M Member_15892067

    I solved this. I needed to put an if statment on useEffect if the user is logged (because I couldn't get his userID in the beginning to send a post request).

    useEffect(() => {
    if(loginStatus === true) {
    loadAllPosts();
    }
    }, [loginStatus])

    Thank you very very much @RichardDeeming for helping me out! Btw, loading parallel in my "context" lets says, is more efficient (memory, loading time, etc) instead of serially? Or is just because using serially will throw all posts if a promise is rejected (fail)?

    JavaScript database php help question javascript

  • Use async function to retrieve data while looping through from another function to insert objects
    M Member_15892067

    Thank you very very much! That works perfectly! But apparently, on postman the data I receive is good, but on frontend the action object inside data is empty and it shouldn't be. This is what I get with postman:

    {
    "data": [
    {
    "id": 2,
    "user_id": 1,
    "text": "d2222222",
    "posted_at": "1/12/2023 1:52:27 PM",
    "likes": 1,
    "comments": 0,
    "picture": "",
    "userid": 1,
    "name": "Gilbert",
    "actions": [
    {
    "id": 1,
    "tweet_id": 2,
    "user_id": 1,
    "liked": 1,
    "retweeted": 0,
    "replied_count": 0
    }
    ]
    }
    ]
    }

    And on frontend (Reactjs):

    {
    "data": [
    {
    "id": 2,
    "user_id": 1,
    "text": "d2222222",
    "posted_at": "1/12/2023 1:52:27 PM",
    "likes": 1,
    "comments": 0,
    "picture": "",
    "userid": 1,
    "name": "Gilbert",
    "actions": []
    }
    ]
    }

    useEffect(() => {
    loadAllPosts();
    }, [])

    function loadAllPosts() {
        Axios.post('/api/loadposts', {
            userID: loginData.id
        }).then((response) => {
            console.log(response.data.data)
            setPosts(response.data.data);
        }).catch((error) => {
            console.log("Something went wrong.. We are investigating.");
        })
    }
    
    JavaScript database php help question javascript

  • Use async function to retrieve data while looping through from another function to insert objects
    M Member_15892067

    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
    
    JavaScript database php help question javascript
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups