2012-08-17 32 views
0

我正在使用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}); 
+0

表中的「id」列的聲明類型是什麼? – ebohlman 2012-08-17 05:39:26

+0

@ebohlman uniqueidentifier – 2012-08-17 07:16:39

+0

如果將其轉換爲SQL中的字符串,會發生什麼情況? – ebohlman 2012-08-17 18:13:16

回答

1

你可能會出現一些字符編碼不匹配。嘗試將id轉換爲SQL本身的字符串;這應該強制正確的編碼。