简体   繁体   English

如何仅使用PG和Node返回PostgreSQL数据库结果?

[英]How to return PostgreSQL database result by using PG and Node only?

I am trying to give myself a practice to just use pure node as my server and PG to connect to PostgreSQL. 我正在尝试给自己一个实践,将纯节点用作服务器,并使用PG连接到PostgreSQL。 What I want to do here is to return the result after querying from database and know it is problem of async. 我要在这里做的是从数据库查询后返回结果,并知道这是异步问题。 So how could I return the value from the callback function inside client.query. 因此,如何从client.query内部的回调函数返回值。

function showAllMovies(){


pg.connect(connectionString, function (err, client, done) {
    if (err) {
      console.log('Can not log into database');
    } else {
      console.log('Connect to database...');
      client.query('SELECT * FROM movies', function (err, result) {
        done();  // client idles for 30 seconds before closing
        var result = JSON.stringify(result.rows[0].movie);
        console.log(result);
        return result;
      });
    }
  });
  pg.end();
}

You can't return the value directly. 您不能直接返回该值。 You need to add a callback of your own to your function, which you will need to call, and provide the result to that callback. 您需要向函数添加自己的回调,然后将其提供给该回调。

For instance: 例如:

function showAllMovies(callback)
{
  pg.connect(connectionString, function (err, client, done)
  {
    if (err)
    {
      console.log('Can not log into database');
    }
    else
    {
      console.log('Connect to database...');
      client.query('SELECT * FROM movies', function (err, result)
      {
        callback(result.rows[0].movie);
        done();
      });
    }
  });
}

In this case, you can use it in your response handler like this: 在这种情况下,您可以像下面这样在响应处理程序中使用它:

showAllMovies(function(movie) { response.write('<li>'+movie+'</li>'); });

Note that this is just an example, you should actually do HTML escaping before output. 请注意,这只是一个示例,您实际上应该在输出之前进行HTML转义。

Alternatively, if you're responding to a request and using Express, you can simply pass down the response object and call one of its methods. 另外,如果您要响应请求并使用Express,则可以简单地传递响应对象并调用其方法之一。 For instance: 例如:

app.get('/movies', function(req,res)
{
  pg.connect(connectionString, function (err, client, done)
  {
    if (err)
    {
      console.log('Can not log into database');
    }
    else
    {
      console.log('Connect to database...');
      client.query('SELECT * FROM movies', function (err, result)
      {
        res.write('<li>'+result.rows[0].movie+'</li>');
        done();
      });
    }
  });
});

The same HTML escaping warning applies here. 相同的HTML转义警告在此处适用。

暂无
暂无

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

相关问题 如何使用 node-postgres (pg) 将此 JSONB 正确保存到 PostgreSQL 中? - How do I properly save this JSONB into PostgreSQL using node-postgres (pg)? 如果使用pg时,如果我从函数返回查询结果,返回未定义,但是如果我用控制台记录它,它就会打印出来呢? - How come when using pg if i return the result of my query from a function, it returns undefined, but if i console log it, it prints? 如何为Node和pg-promise分开控制器和数据库查询 - How to separate controller and database queries for Node and pg-promise 从node.js中的PostgreSQL查询返回查询结果状态 - Return query result state from PostgreSQL query in node.js Node.js 如何将数据从 pg 数据库保存到变量中 - Node.js How to save data from pg database into variable Adonis 如何使用 where 仅返回 with 方法的结果 - Adonis how to return only the result of the with method using the where 无法使用 pg-promise 在 NodeJS 中查询 PostgreSQL 数据库 - “关系不存在” - Unable to query PostgreSQL database in NodeJS using pg-promise - "relation does not exist" 如何允许节点pg模块与Windows postgresql服务器通信? - How do I allow the node pg module communicate with my Windows postgresql server? 如何使用节点 js 仅返回 mongodb 中字段的值。 - How to return only value of a field in mongodb using node js. 进行双重等待时如何仅返回一个结果 Node.js - How to return only one result when making a double await Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM