英文:
Query returns string instead of int using Typescript/Javascript
问题
async function SQLQuery(query: string, database: string) {
try {
// Connects to custom pool manager
const pool = await poolManager.get(database, dbconn[database]);
// Performs query
var result = await pool.request().query(query);
console.log("Result: " + result?.recordset);
return result?.recordset;
} catch (err) {
console.log("Error during SQL query: \n" + err);
}
};
当我运行查询(例如:Select * from UserTable Where userid=856
)时,我会得到以下结果:
{"userid":"856",
"username":"username123",
"date":"2022-06-02T13:04:00.88"}
我能够获取每次运行查询的正确值,但这些查询的结果始终以字符串形式返回。我需要它们以INT(或bigint)的形式返回,并且想知道是否可能实现这一点。如果我还能重新格式化日期就更好了。
当我运行调试器并运行查询时,result 变量知道每个列的类型是什么。例如,id 列的类型为 BigInt,可以通过 result.recordset.columns.id.type 查看。
由于我有一个大量已定义的查询库,我无法更改查询本身或手动根据列名将其转换为int。我需要在查询完成后将其返回为int。
我查看了他们提供的文档和互联网上的信息,但没有找到解决此问题的方法。https://www.npmjs.com/package/mssql
如果有人知道如何解决这个问题,那就太好了!
谢谢。
查阅在线信息,找到有人遇到相同问题。
英文:
Currently I have this typescript/javascript function that connects to my SQL Server and returns data.
async function SQLQuery(query: string, database: string) {
try {
#connects to custom pool manager
const pool = await poolManager.get(database, dbconn[database]);
#Performs query
var result = await pool.request().query(query);
console.log("Result: " + result?.recordset);
return result?.recordset;
} catch (err) {
console.log("Error during SQL query: \n" + err);
}
};
When I run a query (ie: Select * from UserTable Where userid=856
), I get the following result:
{"userid":"856",
"username":"username123",
"date":"2022-06-02T13:04:00.88"}
I am able to get the correct value for each query I run, however the result of these queries are always in string. I need them to return as INT (or bigint) and was wondering if this is possible. It would also be nice if I can reformat the date.
When I run the debugger and run the query, the result variable does know what each column type is. For example, id would say BigInt under result.recordset.columns.id.type
Query result in the variable: result
Since I have a big library of queries that are already defined, I can't change the query itself or manually convert columns to int based off their name. I would need it to return as int after the query is done.
I looked all over this documentation they provide and on the internet but found no luck. https://www.npmjs.com/package/mssql
If someone knows how to resolve this issue it would be great!
Thanks.
Looking through online and finding someone having the same issue.
答案1
得分: 1
JavaScript / TypeScript没有int类型。JS的Number
是一个64位浮点值,因此它可以表示2^53以内的整数,不会丢失精度。
JavaScript确实有一个BigInt类型,但它相对较新,通常不能与JSON一起使用。
因此,一些接口代码将来自其他平台或服务的BigInt转换为JavaScript中的字符串,以确保不会丢失精度。看起来Tedious(mssql包中使用的两个驱动程序之一)这样做。您可以在mssql的文档和GitHub问题中找到有关是否有其他处理方法的额外信息,以及您正在使用的驱动程序。
英文:
JavaScript / TypeScript doesn't have an int type. A JS Number
is a 64-bit floating point value, so it can represent integers up to 2^53 without loss of precision.
JavaScript does have a BigInt type, but it's relatively new and often doesn't work with JSON.
For this reason, some interface code will convert BigInts from other platforms or services into strings in JavaScript, to ensure no loss of precision. It looks like Tedious (one of the two drivers used by the mssql package) does this. You may be able to find additional information in the docs and GitHib issues for mssql and whatever driver you're using on whether there are alternative approaches for handling this.
答案2
得分: 0
可能是因为表格数据类型的原因。为什么不使用泛型类型?它可以包装查询结果。
例如:
interface QueryTypes {
userid: number;
username: string;
date: string;
}
async function SQLQuery(query: string, database: string) {
try {
// 连接到自定义连接池管理器
const pool = await poolManager.get(database, dbconn[database]);
// 执行查询
var result = await pool.request().query<QueryTypes>(query);
console.log("Result: " + result?.recordset);
return result?.recordset;
} catch (err) {
console.log("SQL查询期间出错:\n" + err);
}
}
英文:
It's possibly because of tables data type. Why don't you use generic type? It may wrap the query result.
For Example
interface QueryTypes {
userid: number;
username: string;
date: string;
}
async function SQLQuery(query: string, database: string) {
try {
#connects to custom pool manager
const pool = await poolManager.get(database, dbconn[database]);
#Performs query
var result = await pool.request().query<QueryTypes>(query);
console.log("Result: " + result?.recordset);
return result?.recordset;
} catch (err) {
console.log("Error during SQL query: \n" + err);
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论