如何在不出现控制台错误的情况下检查 API 对象是否未定义?
我正在使用 JS
& AJAX
从 Web-API 收集信息(用于加密矿池)。几乎所有这些都可以完美地工作,但是按照开发人员设置服务器的方式,当不使用某个值时,它可能是空
,而不是0
。其他人确实有占位符值(我正在与开发人员交谈,为所有内容提供占位符,但在那之前,我需要一些帮助)。
例如:
{"version":"0.0.3","statusCode":200,"headers":{"Access-Control-Allow-Headers":"Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods","Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET","Content-Type":"application/json"},"body":{"primary":{"hashrate":{"shared":0,"solo":0},"payments":{"balances":0,"generate":0,"immature":0,"paid":0},"shares":{"shared":{},"solo":{}},"times":{"shared":0},"work":{"shared":0,"solo":0},"workers":{"shared":[],"solo":[]}},"auxiliary":{"hashrate":{"shared":0,"solo":0},"payments":{"balances":0,"generate":0,"immature":0,"paid":0},"shares":{"shared":{},"solo":{}},"times":{"shared":0},"work":{"shared":0,"solo":0},"workers":{"shared":[],"solo":[]}}}}
您可以看到在 body.primary.shares.shared 下它只是 []
而不是零值。
这是脚本的一部分:
function avianDashboard(api, workerAPI) {
const xhr = new XMLHttpRequest();
xhr.open("GET", api, true);
xhr.onload = function () {
if (this.status === 200) {
obj = JSON.parse(this.responseText);
if (typeof (obj.body.primary.shares.shared) === 'undefined') {
document.getElementById('minerShares').innerHTML = 0;
} else {
document.getElementById('minerShares').innerHTML = obj.body.primary.shares.shared.valid;
}
//Rest of the function works properly so I've omitted it for the sake of sanity
当我运行页面时,console.log 显示此..
未捕获的类型错误:无法读取未定义的属性(读取“共享”) 在 XMLHttpRequest.xhr.onload (avian.php:784:61)
并指向这一行:
if (typeof (obj.body.primary.shares.shared) === '未定义') {
我发现了很多应该有助于解决这个问题的东西(包括使用 typeof
因为我最初只是使用 <1
或 null
作为我的条件检查器,但也不起作用。
任何帮助将不胜感激。
I'm using JS
& AJAX
to gather info from a Web-API (for a crypto-mining pool). Almost all of it works perfectly, but with the server the way the developer set it up, when a value is not used, it MIGHT be empty
, not 0
. Others do have a placeholder value (I'm talking with the dev to give everything a placeholder, but until then, I need some help).
e.g.:
{"version":"0.0.3","statusCode":200,"headers":{"Access-Control-Allow-Headers":"Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods","Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET","Content-Type":"application/json"},"body":{"primary":{"hashrate":{"shared":0,"solo":0},"payments":{"balances":0,"generate":0,"immature":0,"paid":0},"shares":{"shared":{},"solo":{}},"times":{"shared":0},"work":{"shared":0,"solo":0},"workers":{"shared":[],"solo":[]}},"auxiliary":{"hashrate":{"shared":0,"solo":0},"payments":{"balances":0,"generate":0,"immature":0,"paid":0},"shares":{"shared":{},"solo":{}},"times":{"shared":0},"work":{"shared":0,"solo":0},"workers":{"shared":[],"solo":[]}}}}
You can see that under body.primary.shares.shared it is just []
and not a zero value.
Here is part of the script:
function avianDashboard(api, workerAPI) {
const xhr = new XMLHttpRequest();
xhr.open("GET", api, true);
xhr.onload = function () {
if (this.status === 200) {
obj = JSON.parse(this.responseText);
if (typeof (obj.body.primary.shares.shared) === 'undefined') {
document.getElementById('minerShares').innerHTML = 0;
} else {
document.getElementById('minerShares').innerHTML = obj.body.primary.shares.shared.valid;
}
//Rest of the function works properly so I've omitted it for the sake of sanity
When I run the page, the console.log shows this..
uncaught TypeError: Cannot read properties of undefined (reading 'shared')
at XMLHttpRequest.xhr.onload (avian.php:784:61)
and points to this line:
if (typeof (obj.body.primary.shares.shared) === 'undefined') {
I've found a lot of things that are supposed to help with this issue (including using typeof
because I was originally just using <1
or null
as my conditional checker and that didn't work either.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
可选链接
来检查对象的属性是否确实存在:在此处查看更多信息< /a>.
You can use
optional chaining
to check if properties of object does exist:Check more info here.