如何使用node-tds从uniqueidentifier字段返回字符串?
问题描述:
我正在使用nodejs + node-tds连接到SqlServer 2008r2 express数据库。我使用uniqueidentifier检索一个对象,并将整个对象作为json返回。如何使用node-tds从uniqueidentifier字段返回字符串?
我可以检索该行很好,但是当我尝试序列化JSON的响应,它出来时髦:
{
"Id": "�<�E�ԃM��\u0000ؚ��J",
"RealName": "Zachary Yates"
}
下面是我使用的代码:
var q = conn.createStatement("select u.Id, u.RealName from [User] u where u.Id = @id;",
{
id: { type: "uniqueidentifier" }
});
q.on("row", function(row)
{
var user =
{
Id: row.getValue("Id").toString()
, RealName: row.getValue("RealName")
};
res.json(user);
});
q.execute({id: uid});
答
你可能会出现一些字符编码不匹配。尝试将id
转换为SQL本身的字符串;这应该强制正确的编码。
表中的“id”列的声明类型是什么? – ebohlman 2012-08-17 05:39:26
@ebohlman uniqueidentifier – 2012-08-17 07:16:39
如果将其转换为SQL中的字符串,会发生什么情况? – ebohlman 2012-08-17 18:13:16