Postgres中的查询问题

时间:2019-04-26 23:07:43

标签: node.js database postgresql

我在Node.js应用程序中使用以下代码段尝试查询(本地)postgres数据库:

   var conString = "postgres://user:password@localhost:5432/mydatabase";
   var client = new pg.Client(conString);

    client.connect(function(err) {
      if(err) {
        return console.error('could not connect to postgres', err);
      }
      client.query("SELECT * FROM users WHERE name = $1 AND cred = $2", [String(req.body.usr), String(req.body.pword)], function(err, result) {
        if(err) {
          return console.error('error running query', err);
        }
         if (typeof result.rows[0] === "undefined") {
         console.log("No user/password determined in DB for login attempt");
         } else {

         }  //user/password is 'undefined' (NOT found in database)...OR NOT... 

        client.end();
      });
    });

查询运行时收到错误消息...我相信问题可能出在我的查询调用中...如果是这样的话(或者是其他语法问题),有人可以通知我如何更改代码以正确执行查询吗?

我只需要从表单(req.body.usr和req.body.pword)中获取2个(用户提供的)结果,并将它们与数据库表“用户”进行比较,以确定凭据是否正确。我已经相信数据库连接可以正常工作。任何建议,不胜感激。我先谢谢你。

1 个答案:

答案 0 :(得分:0)

更改:

async asyncData () {
    let { data } = await axios.get('http://localhost:3333/api/users')
    return { users: data.users } //or however your 'data' comes back. Try console.log(data) to see.
  }

收件人:

client.query("SELECT * FROM users WHERE name = $1 AND cred = $2", [String(req.body.usr), String(req.body.pword)], function(err, result) {...

了解更多:https://node-postgres.com/features/queries

相关问题