Monday 13 March 2017

PSQL nodejs variable undefined

i have a problem with my function to get all articles from my database.

Articles database: id | title | price | description

images database : id | articleid | name

When i log the data 'articles' it's undefined.

client.query('SELECT * FROM articles', function (err, result) {
        if (err) throw err;

        if (result.rowCount != 0) {
            var articles = [];
            result.rows.forEach(function (entry) {
                client.query('SELECT name FROM images WHERE articleid = $1', [entry.id], function (err, result) {
                    if (err) throw err;

                    var article = [];
                    var images = [];

                    result.rows.forEach(function (entry) {
                        images.push(entry.name);
                    });

                    article = entry;
                    article.images = images;

                    articles.push(article);

                });
            });
            console.log(articles);
            res.send(articles);
        }

        res.send();
    });

Thank you for your help



via MrNuTruT

No comments:

Post a Comment