控制台上显示的错误是:
TypeError: Cannot read property '0' of null
at app.post (/root/src/master.js:24:29)
master.js 24:29就是这个
let outputs = req.body.outputs
let outs = []
for (let out of outputs) {
outs.push({
address: out.addresses[0],
value: out.value
})}
}
第24行是地址:
out.addresses[0],
来自这个json的请求
"outputs": [
{
"value": 10000,
"script": "a91402f42dce015def2fab0b1d6f7f6c742bae66e75e87",
"addresses": [
"31xdnmzVzmAQ5CaWxtiDr3jwCFfeUpHEb1"
],
"script_type": "pay-to-script-hash"
},
{
"value": 1020,
"script": "0014396ef6487043092385338ccfe9d327fb031975d6",
"addresses": null,
"script_type": "pay-to-witness-pubkey-hash"
}
]
有人可以帮我解决这个问题吗?非常感谢!
答案 0 :(得分:1)
地址为null。您可以添加空检查
let outputs = req.body.outputs
let outs = []
for (let out of outputs) {
outs.push({
address: out.addresses ? out.addresses[0] : '',
value: out.value
})}
}
答案 1 :(得分:0)
由于您的addresses
可以为null(根据您共享的示例输出),
for (let out of outputs) {
outs.push({
address: out.addresses ? out.addresses[0] : [], //notice the change here
value: out.value
})}
}
如果out.addresses
为null
,请设置空数组。
答案 2 :(得分:0)
address
在对象中的一个位置为空,
因此,您可以使用address
运算符添加条件以检查present
terniary
是否为let outputs = req.body.outputs
let outs = []
for (let out of outputs) {
outs.push({
address: out.addresses ? out.addresses[0] : '',
value: out.value
})}
}
。
address: out.addresses ? out.addresses[0] : '',
<强> The above line checks if
强>
my_variable = "beginning of the string" \
"end of the string"
out.addresses`存在,如果是,它将返回else返回空字符串。