为什么node
我是node.js和javascript研究的新手,我正在尝试构建一个后端以从Firebird数据库中获取信息。
我正在使用我所遵循的课程中的某些依赖项。我的项目为“表达”,“ nodemon”,“ node-firebird”。
使用以下结构:
Project structure
我的server.js文件中的代码是:
const firebird = require('node-firebird');
const express = require('express');
const app = express();
//const teste;
const options = {
host: '127.0.0.1',
database: 'C:/Users/alexandrefelix.GREENCANE/Desktop/Aulas/backend-firebird/src/TGA.FDB',
user: 'SYSDBA',
password: 'masterkey',
};
app.get('/', (req, res) => {
// 5 = the number is count of opened sockets
var pool = firebird.pool(5, options);
// Get a free pool
pool.get(function(err, db) {
if (err)
throw err;
// db = DATABASE
db.query('SELECT * FROM GEMPRESA', function(err, result) {
// IMPORTANT: release the pool connection
console.log(result);
db.detach();
});
});
// Destroy pool
pool.destroy();
return res.json({ message: 'Hello World!'});
});
app.listen(3333);
因此,当我运行代码并在浏览器中访问“ localhost:3333”时,它会显示我的测试消息(Hello World),但在控制台中,它会将varchar字段显示为缓冲区:
城市:缓冲区55 42 45 52 41 42 41
何时应该:
城市:“ UBERABA”
我的问题是为什么返回的JSON以这种方式显示VARCHAR字段,以及如何将列的结果放入变量中。
回答如下:结果实际上是需要解析的resultSet
result.forEach( function(row) {
console.log( row.id, ab2str(row.name)); //id and name are fields from the select *
});
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
更多信息:https://github/hgourvest/node-firebird/wiki/Example-of-querying-using-Promises