0

What does it do? In the documentation, I do not understand the answer.

I'm not using it correctly, I get a query error from the database

 pool.connect(function (err, client, done) {
    if (err) console.log("connect " + err.toString());
    else
        client.query('SELECT id, "idName", "idContact", "idExperience", 
"idSkill", "dateAdded", "dateColloquy"' +
            'FROM public."applicant ";', function (err, result) {

            if (err) {
                console.log("query " + err.toString());

            }
            else {
                console.log(result.rows);
                //module.exports.res = result.rows;
            }
            done();
        });

});

1 Answers1

0

Now there are many errors, the error you are getting from the database (sad that you don't share it with us) should be the "dateColloquy"FROM part. Now once you fixed that and inserted a space before FROM, you'll notice that all your fields are not included as data, but exactly as what you wrote - idName (literally the string), ... for every field but id the same.

Use backticks (```) (or nothing at all) for column names, not " nor '.

The next error you'll experience will be that you are trying to

module.exports.res = result.rows;

You can't export asynchronous retrieved data like that.

baao
  • 71,625
  • 17
  • 143
  • 203
  • This is a separate file. how to be, if I specify at the end of the function module.exports for app.js file, I'm using undifine. how to transfer database data to another file? – Антон Прохоров Jun 27 '18 at 19:58
  • You can with a callback and an exported function. You can't export asynchronous retrieved data with module.exports though. It's pretty much the same as in https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call @АнтонПрохоров and how to deal with async code. `function foo(callback) { setTimeout(() => callback('foo'), 1000); } function bar(res) { console.log(res); } foo(bar);` – baao Jun 27 '18 at 20:22
  • @MikeMcCaughan The lib is unrelated to the question, it's good that OP has omitted to tag it. The question is about async behavior and a few syntax errors in sql query. FWI the lib seems to be node-mysql if that helps you.. – baao Jun 27 '18 at 20:24
  • Of course, you have to know that the function is asynchronous, and to know that, you need to know the library. I'm also not sure how you know that the line `module.exports.res = result.rows` is used... it's not in the question. – Heretic Monkey Jun 27 '18 at 20:29
  • Take another look at the question please, also on the commented parts... @MikeMcCaughan To know that it's async it's sufficient to see all the callbacks – baao Jun 27 '18 at 20:30
  • Mayby use this code const { Pool } = require('pg') module.exports = { query: (text, params) => pool.query(text, params) } – Антон Прохоров Jun 28 '18 at 18:31