3

I am using node.js and make database connection with postgresql. My dbConfig.js look like :-

var pg = require('pg');
var client  = new pg.Client({
    host:'myhoost',
    port:'5432',
    user:'myuser',
    password:'mypass',
    database:'mydb',
    ssl:true
});
client.connect();
module.exports.myconnection = client;

my api.js file look like :-

var dbConnect = require('./dbConfig.js');
var client = dbConnect.myconnection;
var ser = function(value) {
    var query = "SELECT * FROM tbl_api WHERE apikey = '" + value + "'";

    client.query(query, function(err, result) {
        var res = true;
        if (err) {
            var res = false;
        } else {
            if (result.rowCount > 0) {
                res = true;
            } else {
                res = false;
            }
        }
       return res;
    });
};
module.exports.checkAPI = ser;

my api.js look like:-

var express = require('express');
var app = express();
var apiCheck = require('./api.js');


//APIKey Generator check api
app.get('/apicheck/:apikey', function(request, response) {
    var value = request.params.apikey;
    var result = apiCheck.checkAPI(value);
    response.send(result);
});

i want the res from client.query into the result variable. I found one same issue with mysqljs issue but still i am not able to solve this doubt.

5
  • What response do you receive? Commented Jun 16, 2017 at 6:04
  • I am receiving nothing. @Zeokav Commented Jun 16, 2017 at 6:06
  • Why somebody down voted this question ? I am just asking my doubt. is it valid or not ? Commented Jun 30, 2017 at 7:16
  • 1
    Don't you just hate it when people downvote without criticism? It's a valid question for people not aware of the async nature of node. Commented Jun 30, 2017 at 7:41
  • Thanks bro @Zeokav Commented Jun 30, 2017 at 9:26

1 Answer 1

2

We need the callback to finish to return a response, one of the way to solve this is using promises as shown below:

var ser = function(value) {
    var query = "SELECT * FROM tbl_api WHERE apikey = '" + value + "'";
    return new Promise(function (resolve, reject) {
       client.query(query, function(err, result) {         
           if (err) {
              return reject(err);
           } else {
              if (result.rowCount > 0) {
                  return resolve(true);
              } 
           }
           return resolve(false);
       });
   });
};
module.exports.checkAPI = ser;

And in your api.js changes to:

var express = require('express');
var app = express();
var apiCheck = require('./api.js');


//APIKey Generator check api
app.get('/apicheck/:apikey', function(request, response) {
    var value = request.params.apikey;
    apiCheck.checkAPI(value)
      .then(function(result) { response.send(result); })
      .catch(function(err) { response.send(err); });
});
Sign up to request clarification or add additional context in comments.

7 Comments

what is promises ? like uses and all ? @nash_ag
It give me an error as TypeError: Cannot read property 'then' of undefined.
Ahh - fixing the answer.
@DhiralKaniya Here are more details on promises - strongloop.com/strongblog/…
I got my error. Sorry for above.. but is it possible to take res value inside some variable in web.js like var result = apiCheck.checkAPI(value)
|

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.