[英]JSON returning undefined values
我在返回未定義的json
輸出值時遇到問題。
JSON:
[ { ID: 2, user: 'ngx.daniel', password: 'password', admin: 'F' } ]
碼:
console.log(res);
res = JSON.stringify(res);
console.log(res);
var user = {};
user.user = res.user;
user.password = res.password;
user.admin = res.admin;
console.log(user);
OUTPUT:
[ RowDataPacket { ID: 2, user: 'ngx.daniel', password: 'password', admin: 'F' } ]
[ { ID: 2, user: 'ngx.daniel', password: 'password', admin: 'F' } ]
{ user: undefined, password: undefined, admin: undefined }
一些東西。 如果這是您的json:
[ RowDataPacket { ID: 2, user: 'ngx.daniel', password: 'password', admin: 'F' } ]
您不需要將其分類。 對其進行字符串化時,會將對象轉換為字符串。 然后,您嘗試訪問它沒有的字符串屬性。
1-刪除字符串
2-Thats是與RowPacketData對象一起返回的數組,該對象也是無效的json。
3-要訪問數組中的對象,您需要首先訪問索引。 所以它真的應該像
(如果res
= [ { ID: 2, user: 'ngx.daniel', password: 'Root_!$@#', admin: 'F' }]
))
var user = {};
user.user = res[0].user;
user.password = res[0].password;
user.admin = res[0].admin;
響應顯然可以包含多個項目( RowDataPackets
)。 您可以通過索引0獲得第一個。並且不需要對響應進行字符串化:
console.log(res);
var user = {};
user.user = res[0].user;
user.password = res[0].password;
user.admin = res[0].admin;
console.log(user);
哪些印刷品
對象{user =“ ngx.daniel”,password =“ Root _!$ @#”,admin =“ F”}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.