我的对象有一个问题 我在节点js中使用存储库来存储数据
这是我的商店数据功能
memored.store('driverList', list);
这是获取商店列表 这是代码
memored.read('driverList', function(err, list) {
var driver = list;
});
获取列表后的就像示例
var list = [ null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
'{"phonenumber":"123123","client":{"index":"103.240.34.250:61667","sock":{"_connecting":false,"_handle":{"fd":20,"writeQueueSize":0,"owner":"[Circular]","reading":true},"_readableState":{"highWaterMark":16384,"buffer":[],"length":0,"pipes":null,"pipesCount":0,"flowing":false,"ended":false,"endEmitted":false,"reading":true,"calledRead":true,"sync":false,"needReadable":true,"emittedReadable":false,"readableListening":false,"objectMode":false,"defaultEncoding":"utf8","ranOut":false,"awaitDrain":0,"readingMore":false,"decoder":null,"encoding":null},"readable":true,"domain":null,"_events":{},"_maxListeners":10,"_writableState":{"highWaterMark":16384,"objectMode":false,"needDrain":false,"ending":false,"ended":false,"finished":false,"decodeStrings":false,"defaultEncoding":"utf8","length":0,"writing":false,"sync":false,"bufferProcessing":false,"writecb":null,"writelen":0,"buffer":[],"errorEmitted":false},"writable":true,"allowHalfOpen":false,"onend":null,"destroyed":false,"bytesRead":24,"_bytesDispatched":2,"_pendingData":null,"_pendingEncoding":"","server":{"domain":null,"_events":{},"_maxListeners":10,"_connections":1,"connections":1,"_handle":{"fd":17,"writeQueueSize":0,"owner":"[Circular]"},"_usingSlaves":false,"_slaves":[],"allowHalfOpen":false,"_connectionKey":"4:0.0.0.0:25001"},"_peername":{"address":"103.240.34.250","family":"IPv4","port":61667},"_consuming":true},"_actualLength":0,"_msg":[2,114,116,101,115,116,64,103,109,97,105,108,46,99,111,109,124,114,97,118,105,110,97],"_written":0,"driver":"[Circular]"},"online":true,"onDuty":true,"latitude":0,"longitude":0,"vehicle":"Sedan","id":1056,"fullname":"test01 test01","pendingRides":{},"ride":null,"rpm":"0","licensePlate":"dri123","logintime":1471025147}' ]
var data = list.filter(Boolean).shift();
var list = JSON.parse(data)
console.log(list.id)
for (var elementId in list) {
driver = list[elementId];
if(!driver.online || !driver.onDuty) continue;
driverLists.push({
id: driver.id,
latitude: driver.latitude,
longitude: driver.longitude,
fullname: driver.fullname,
rpm: driver.rpm,
vehicle: driver.vehicle,
licensePlate: driver.licensePlate,
logintime: driver.logintime
});
}
console.log(driverLists);

我想要来自这个对象的driver.id但是当我试图让它得到我未定义时我也尝试使用JSON.parse(value)所以它得到我的SyntaxError:Unexpected token,
所以我怎么能得到身份证明 如果你有适当的解决方案,请告诉我
答案 0 :(得分:0)
driver
是一个数组,driver[15]
是一个字符串。
所以也许你想要JSON.parse(driver[15]).id
?
答案 1 :(得分:0)
看起来driver
是一个数组,由输出中的[]
括号看到,对象实际上是一个字符串。
首先删除因某些原因而拥有的所有null
值,然后获取字符串并解析它,然后获取值
var data = driver.filter(Boolean).shift();
var parsed = JSON.parse(data);
var id = parsed.id;