简体   繁体   English

使用node.js,pg-promise的简单检查功能

[英]Simple Check Function with node.js, pg-promise

I am trying to write a simple check() function following this example . 我试图在此示例之后编写一个简单的check()函数。 The function successfully prints out "True!" 该功能成功打印出“ True!” but the return value of the function is undefined when I do console.log(submitModels.verifyOrganizationString(formInputs.organizationString)); 但是当我执行console.log(submitModels.verifyOrganizationString(formInputs.organizationString));时,该函数的返回值是undefined console.log(submitModels.verifyOrganizationString(formInputs.organizationString)); . How can I get the function to return true and false ? 如何获得返回truefalse的函数?

Right now the function only returns "Invalid organization string" even when the string is valid. 现在,即使该字符串有效,该函数也仅返回"Invalid organization string"

I am thinking this result has to do with the pg-promise library, but am not sure. 我认为这个结果与pg-promise库有关,但不确定。

In the file submit.js , I have: 在文件submit.js ,我有:

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");
  }

}

The reason you're getting undefined is because 'verifyOrganizationString' doesn't actually return anything. 之所以变得未定义,是因为“ verifyOrganizationString”实际上未返回任何内容。 You need to return the promise created by your db.one chain. 您需要返回由db.one链创建的promise。

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. 尽管在这种情况下该方法仍然不会返回true或false,但是布尔值包装在另一个promise中,以便您可以像对db.one的结果一样,将verify函数的结果链接起来。

Your problem is due to both invalid use of promises and bad use of pg-promise . 您的问题是由于对promises的无效使用和对pg-promise的错误使用造成

If you want the function to return true / false depending on whether a record is found, change the function to this: 如果您希望函数根据是否找到记录返回true / false ,请将函数更改为:

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. 另请参见: SELECT⇒INSERT示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM