我是node.js和javascript研究的新手,我正在尝试建立一个从火鸟数据库中获取信息的后端。
我正在使用我正在学习的课程中的一些依赖项。我的项目是'express','nodemon','node-firebird‘。
使用以下结构:
我的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
当它应该是:
城市:“乌贝拉巴”
我的问题是,为什么返回的JSON以这种方式显示VARCHAR字段,以及如何将列的结果放入变量中。
发布于 2019-10-09 20:27:34
结果实际上是一个需要解析的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.com/hgourvest/node-firebird/wiki/Example-of-querying-using-Promises
发布于 2021-02-09 05:22:38
这是转换所有缓冲区属性的另一种方法。
async function query(aSql, aParams = []){
return new Promise( (resolutionFunc,rejectionFunc) => {
Firebird.attach(options, function(err, db) {
if (err){
rejectionFunc(err);
}
// db = DATABASE
db.query(aSql, aParams, function(err, result) {
try{
if(err){
rejectionFunc(err);
}
for (let i = 0; i < result.length; i++) {
for (const [key, value] of Object.entries(result[i])) {
if(Buffer.isBuffer(value)){
result[i][key] = `${value}`; // converting buffer to string
}
}
}
resolutionFunc(result);
}finally{
db.detach();
}
});
});
});
}https://stackoverflow.com/questions/58303870
复制相似问题