node.js - 为什么在 node.js 中运行这个查询会阻止进程退出?

标签 node.js

为什么使用 pg module 运行查询阻止进程终止?

这段代码直接取自 github 页面的自述文件。在终端中执行此 node.js 脚本会执行 sql 查询,但不会立即返回到 bash 提示符。退出可能需要大约 20 秒。

var pg = require('pg'); 
var conString = "tcp://postgres:1234@localhost/postgres";

//error handling omitted
pg.connect(conString, function(err, client) {
  client.query("SELECT NOW() as when", function(err, result) {
    console.log("Row count: %d",result.rows.length);  // 1
    console.log("Current year: %d", result.rows[0].when.getYear());
  });
});

最佳答案

你只需要调用pg.end

关于node.js - 为什么在 node.js 中运行这个查询会阻止进程退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8090548/

相关文章:

jquery - 使用 mysql 和 nodejs 更新查询时出现 ER_PARSE_ERROR

node.js - 如何使用 Node.js 创建自定义对象

node.js - 检查 node.js 中是否存在符号链接(symbolic link)

mysql - NodeJS MySQL 转储

javascript - 为什么 Safari 会复制 GET 请求而 Chrome 不会?

javascript - 如何从异步 Mongoose 中间件后 Hook 抛出错误

node.js - Nodejs 中将毫秒转换为格式化日期

node.js - Sequelize save() 问题

node.js - 如何配置 Chocolatey 以使用公司代理?

node.js - 如何正确使用mongoDB中的查找和更新?