Node.js - Function returning the query value?

Asked

Viewed 1,050 times

2

It is the following I tried to search but I did not find any solution, to try to create a function in the model of my application that will return the data of the query in the database. This function would be called in my controller which would later be sent to the page file.

Function making the query

exports.show = function(){
    var result;
    connection.getConnection(function(err, connection){
        connection.query('select 1 + 1 as teste', function(err, rows){
            this.result = rows[0].teste;
            connection.release();
            console.log('---->', this.result);
            return this.result;
        });
    });
};

Controller

var Model = require('../models/Models');

exports.Index = function(request, response){
    response.pageInfo = {};

    response.pageInfo.title = 'Users';
    response.pageInfo.users = Model.show(); 
    response.render('user/index', response.pageInfo);
};

And the page . jade

extends ../layout
block content
    h1 #{users}

And by executing it this way it simply doesn’t print the value on the page, but on the console it works.

1 answer

2


I’ll answer without being sure, because I don’t know for example what comic you’re using.

The first problem I see is when you call:

response.pageInfo.users = Model.show(); 

This will start code that is asynchronous. That is, this code will make a database call but the rest of the code will not wait! That is to say: response.render('user/index', response.pageInfo); will be run before the comic answer comes back.

Then there’s another problem there: when you do it return this.result; this will return nothing to response.pageInfo.users =. First because it is within 2 functions and Return does not make both give return, only the one where the return and second because it is asynchronous and response.pageInfo.users = has already value and the code has already run.

What to do?

Uses a callback that allows you to use the information/data when it’s ready.

I think in Jade you have to use the data in the second parameter of response.render and not as property of response.

Code suggestion:

exports.show = function(callback){
    var result;
    connection.getConnection(function(err, connection){
        connection.query('select 1 + 1 as teste', function(err, rows){
            this.result = rows[0].teste;
            connection.release();
            console.log('---->', this.result);
            return callback(this.result);
        });
    });
};

Controller

var Model = require('../models/Models');

exports.Index = function(request, response){
    Model.show(function(users){
        response.render('user/index', {
            users: users,
            title: 'Users'
        });
    }); 
};
  • 1

    That’s right, man. I had done in a way that when loading the page for the second time appeared the result of the database (Mysql), but I did following its instructions and it worked here. Thank you!!!!!! (I started studying Node.js two days ago and I’m already very excited about it)

  • @Liw.S. great! If you want you can mark the answer as accepted :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.