gpt4 book ai didi

javascript - 访问 JSON 字符串时出现类型错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:18 25 4
gpt4 key购买 nike

我正在使用http://blockchain.info/api/api_websocket Websocket API。

我的每笔交易的 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 访问“addr”变量,它在控制台中打印得很好。

但是当我通过 Mongoose 运行命令时:

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);

最佳答案

看起来 json 变量在您调用 Game.findOne 的范围内不可用。

关于javascript - 访问 JSON 字符串时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15171239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com