0

I am trying to write a simple check() function following this example. The function successfully prints out "True!" but the return value of the function is undefined when I do console.log(submitModels.verifyOrganizationString(formInputs.organizationString));. How can I get the function to return true and false?

Right now the function only returns "Invalid organization string" even when the string is valid.

I am thinking this result has to do with the pg-promise library, but am not sure.

In the file submit.js, I have:

function verifyOrganizationString(organizationString) {
  db.one("select * from organizations where organization_string=$1", [organizationString])
    .then(data => {
        console.log("True!");
        return true;
    })
    .catch(error => {
        console.log("False!");
        return false;
    });
}

module.exports = {
  verifyOrganizationString,
};

In another file, I have

const submitModels = require('../models/submit.js');

function proccessSubmission(req, res) {
  var formInputs = req.body;
  console.log(submitModels.verifyOrganizationString(formInputs.organizationString));

  if (submitModels.verifyOrganizationString(formInputs.organizationString)) {
    submitModels.insertIntoDatabase(formInputs);
    res.redirect('/');
  } else {
    res.send("Invalid organization string");
  }

}
Community
  • 1
  • 1
cpage
  • 119
  • 6
  • 27
  • 1
    you need to clear your concept about promises because you are using promises illegally sort of anti-pattern. Remember one thing the values returned from any of `.then` or `.catch` will only be accessed by the next chained `.then` or `.catch` this is because the values returned is actually a promise. – nurulnabi Mar 18 '17 at 05:02
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – t.niese Mar 18 '17 at 15:24

2 Answers2

2

The reason you're getting undefined is because 'verifyOrganizationString' doesn't actually return anything. You need to return the promise created by your db.one chain.

Although the method will still not return true or false in that case but the Boolean wrapped in another promise so that you can chain the result of your verify function the same way you did with the result of db.one.

Christopher
  • 856
  • 9
  • 16
  • Would the better approach be [Chaining Queries](https://github.com/vitaly-t/pg-promise/wiki/chaining-queries)? – cpage Mar 18 '17 at 04:49
  • @cpage that's nothing to do with your broken code ;) Your problem is that you misuse promises. You need to learn how to use promises correctly. – vitaly-t Mar 18 '17 at 13:31
2

Your problem is due to both invalid use of promises and bad use of pg-promise.

If you want the function to return true/false depending on whether a record is found, change the function to this:

function verifyOrganizationString(organizationString) {
    return db.oneOrNone("select * from organizations where organization_string = $1",
                         organizationString, a => !!a);
}

Then you can use it as one would a regular promise:

verifyOrganizationString('bla-bla')
    .then(data => {
       // data = true/false
    })
    .catch(error => {
        // error
    });

See also: SELECT ⇒ INSERT examples.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138