嗨我有一个问题,如果在使用data.driver.id之前使用node.js中的memored包之前正常工作,那么它就像1010那样得到我,但现在它给了我未定义的
这是我的所有细节的对象
var data = { event: 2,
driver: '{"phonenumber":"123123","client":{"index":"103.104.34.250:59555","sock":{"_connecting":false,"_handle":{"fd":21,"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":59555},"_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":1470925503}' }
console.log(data.driver.id);
我添加了我的代码,所以我想得到所有与驱动程序相关的属性,如id,fullname,logintime,纬度,经度所以请用这个错误来帮助我
答案 0 :(得分:1)
您需要先解析JSON。试试吧:
var data = { event: 2,
driver: '{"phonenumber":"123123","client":{"index":"103.104.34.250:59555","sock":{"_connecting":false,"_handle":{"fd":21,"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":59555},"_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":1470925503}' }
console.log(JSON.parse(data.driver).id);
答案 1 :(得分:1)
有人建议解析字符串以将其转换为对象。但它已经是一个对象了。只需删除单引号。
var data = { event: 2,
driver: {"phonenumber":"123123","client":{"index":"103.104.34.250:59555","sock":{"_connecting":false,"_handle":{"fd":21,"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":59555},"_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":1470925503} }
console.log(data.driver.id);