1

I am having a scenario where I have to display a form and rather than taking input, I have to display some value extracted from database using nodejs. Below is the expected outcome

enter image description here

So, I wrote the following code

confirm.ejs

<form>
<label for="fname">First Name</label>
<input type="text" class="form-control" value="<%= ticket.Fname %>" id="fname" readonly>
</form>

js but I believe it is correct

router.get("/confirm",function(req,res){
    db.query("SELECT * FROM details WHERE Ticket=?",[variabled4],function(err, results, fields){
        if (err) throw err;
        res.render('confirm', { title: 'ticket info', ticket: results});
    });
});

So, I think the problem is in value = "<%= ticket.Fname %> and the whole form is displayed but with empty fields. (Fname is a column in details table with hello as value, not to be confused as a typo of fname)

Please suggest how to fix this.

4
  • the mdn website suggests HTMLInputElement but I don't know how to use that Commented May 23, 2021 at 11:41
  • 1
    sequelize Raw Queries should return an array as response. Check if you need to extract ticket from it ({ title: 'ticket info', ticket: results[0]}). Commented May 23, 2021 at 12:38
  • @Daniel thank you very much. At first, I thought you were selecting the specific column in the array but then I understood that you were selecting the array itself. I will try again using this edit. Commented May 23, 2021 at 13:02
  • @Daniel thanks the question was solved and there was no issue with the ejs inside html tag that I thought. You can post an answer also. Commented May 23, 2021 at 13:05

1 Answer 1

1

sequelize Raw Queries returns an array as response. You should check if the array is not empty and extract the ticket for results[0]

See: https://sequelize.org/master/manual/raw-queries.html

Here a small snippet (Handel the "ticket not found" as you see fit)

router.get("/confirm",function(req,res){
    db.query("SELECT * FROM details WHERE Ticket=?",[variabled4],function(err, results, fields){
        if (err) throw err;
         // on "results.length == 0" handle "ticket not found" flow

        let ticket = results[0];
        res.render('confirm', { title: 'ticket info', ticket: ticket});
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.