2013-03-02 83 views
1

我正在使用http://blockchain.info/api/api_websocket websocket api。訪問JSON字符串時的TypeError

我的每個事務JSON對象看起來像這樣:

{ 
    "op": "utx", 
    "x": { 
     "hash": "f6c51463ea867ce58588fec2a77e9056046657b984fd28b1482912cdadd16374", 
     "ver": 1, 
     "vin_sz": 4, 
     "vout_sz": 2, 
     "lock_time": "Unavailable", 
     "size": 796, 
     "relayed_by": "209.15.238.250", 
     "tx_index": 3187820, 
     "time": 1331300839, 
     "inputs": [ 
      { 
       "prev_out": { 
        "value": 10000000, 
        "type": 0, 
        "addr": "12JSirdrJnQ8QWUaGZGiBPBYD19LxSPXho" 
       } 
      } 
     ], 
     "out": [ 
      { 
       "value": 2800000000, 
       "type": 0, 
       "addr": "1FzzMfNt46cBeS41r6WHDH1iqxSyzmxChw" 
      } 
     ] 
    } 
} 

我訪問通過使用json.x.out的「地址」的變量[0] .addr打印出精美的控制檯。

然而,當我通過貓鼬運行命令:

Game.findOne({address:json.x.out[0].addr},function (err, game) { 

導致以下錯誤:

TypeError: Cannot read property 'x' of undefined 
    at Promise.<anonymous> (/Users/michael/Desktop/DugleyBit/app.js:176:25) 
    at Promise.addBack (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/promise.js:133:8) 
    at Promise.EventEmitter.emit (events.js:96:17) 
    at Promise.emit (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/promise.js:66:38) 
    at Promise.complete (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/promise.js:77:20) 
    at Query.findOne (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/query.js:1607:15) 
    at model.Document.init (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/document.js:227:11) 
    at model.init (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/model.js:196:36) 
    at Query.findOne (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/query.js:1605:12) 
    at exports.tick (/Users/michael/Desktop/DugleyBit/node_modules/mongoose/lib/utils.js:404:16) 

這難道不是一個字符串?我如何轉換它?

感謝

編輯:

請注意,JSON是正確格式化:

var json = JSON.parse(message); 
+0

'json'看起來像什麼?該錯誤說明它是未定義的。 – Blender 2013-03-02 05:51:04

+0

嘿@Blender .. CONSOLE.LOG(JSON)返回: '{OP: 'UTX', X: {散列: '6c516bd55e71a7020c33abfa3abfecc4439a2ff26199e20b700b60ec3ed93e38', vin_sz:1, vout_sz:2, lock_time: '不可用' , 尺寸:259, relayed_by: '127.0.0.1', tx_index:57725380, 時間:1362202829, 輸入:[[對象]], 出:[[對象]]}}' – Michael 2013-03-02 05:56:08

回答

0

看起來,你叫Game.findOnejson變量不在範圍內使用。