This is a question about node.js. I'd like to receive the value delivered by the Express Router from Jade's inline JavaScript.

Asked 2 years ago, Updated 2 years ago, 102 views

You are about to receive the rows value passed by the Express Router from Jade's inline JavaScript. '#{rows[1].It's accessible by the code 'id}', but I can't figure out where it's coming from. Also, why is it not accessible from the repetition? Is there any other way to approach it?

node.js code

app.get('/output', function(req, res){
    var sql = 'select * from table';
    conn.query(sql, function(err, rows_){
        if(err){
            console.log(err);
        } } else {
            res.render('output', {rows:rows_})}
    })
});

output.jade code

html
    head
    body
        script.
            alert('#{rows[1].id}') // possible
            for(var i=0; i<5; i++){
                alert('#{rows[i].id}') //Not possible
                };

node.js express jade

2022-09-22 18:01

2 Answers

I don't know what form Young Min's rows data comes in.I'm guessing you used id I randomly put it in a json form in an array and used it. I am attaching the image that I used, so I hope it helps you.


2022-09-22 18:01

I searched for #{JavaScript expression}The grammar was located in the interpolation section of the Jade document. (He doesn't say it explicitly, but just shows it as an example.))

- var msg = "not my inside voice";
p This is #{msg.toUpperCase()}
// // <p>This is NOT MY INSIDE VOICE</p>

You can also access the JavaScript variable in the iteration statement. (It can't be.) If you refer to the Jade documentation, you can try it roughly like this. (I haven't tested it.)

// This view already accepts the JS variable called rows
body
    // When you enter the JS section,
    script.
        for (var i=0; i<5; i++) {
            // I think we can just write it like this.
            alert(rows[i.id + "Burnles.")
        };


2022-09-22 18:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.