Search code examples
mysqlnode.jspugsequelize.js

Can't get the Plain object from Promise


I am using MySQL, Node.js, Pug, and Sequelize.

I want to grab data from the database and convert it into a value that can be placed into the variable that Pug can convert to html.

    app.get('/', function(req, res){

    var name1 = BasicInformation.findOne({
        attributes: [ 'advisor_name' ],
        where: {
            id: '1'
        }
    }).then(function(name1){
        return name1;
    });

        res.render('programofstudy', {
            advisor_name: name1.advisor_name
        });
});

The problem is that nothing appears on the webpage. I have tried name1.advisor_name I have tried name1 I have tried multiple ways of obtaining the information.

I have tried placing the Query inside next too advisor_name: I am just not sure how to get the value from Sequelize and into the Pug variable.


Solution

  • You need to render the page INSIDE the callback from mongodb:

    app.get('/', function(req, res){
    
      var name1 = BasicInformation.findOne({
        attributes: [ 'advisor_name' ],
          where: {
            id: '1'
          }
        }).then(function(name1){
    
          res.render('programofstudy', {
            advisor_name: name1.advisor_name
          });
    
      });
    
    });