0

i want make a helper and will be used in my view (pug), so i send a function as data to the view file, but the problem is, my helper / passed function wont return a value or == undefined in my view, pug, thanks

    app.get('/admin/pengguna', function(req,res){
    db.all("SELECT * FROM main_posts WHERE wdyw='2'", function(err, rows) {
        //var r = db.all("SELECT * FROM main_posts WHERE wdyw='1' AND ")
        if (!err) {
            res.render('admin/pengguna', {
                path: req.path,
                pengguna : rows,
                useringroup: function(w) {
                    db.all("SELECT * FROM main_posts WHERE wdyw='1' AND identity= $w", { $w: w }, function(err, result){
                        return result.lenght;
                    })
                }
            })
        }
    })
})

enter image description here enter image description here

Kasiriveni
  • 5,671
  • 1
  • 22
  • 31
Kunto Handoko
  • 79
  • 1
  • 1
  • 11

1 Answers1

0

Wait for the db to return the data and then render the view. Your view can not make a call directly to db. This is why we use MVC (Model View Controller). You need to fetch all the data in your controller and then pass it to view.

You will need to use something like this,

app.get('/admin/pengguna', function(req,res){
    db.all("SELECT * FROM main_posts WHERE wdyw='2'", function(err, rows) {
        //var r = db.all("SELECT * FROM main_posts WHERE wdyw='1' AND ")
        if (!err) {
            db.all("SELECT * FROM main_posts WHERE wdyw='1' AND identity= $w", { $w: w }, function(err, result){
                res.render('admin/pengguna', {
                    path: req.path,
                    pengguna : rows,
                    useringroup: result.length
                })
            })

        }
    })
})

Also you will need to adjust your code for w. Before you were getting it from the function you passed to the view. You'll need to get its value by any other method or modify the query.

Anurag Awasthi
  • 6,115
  • 2
  • 18
  • 32