[英]Checking for undefined in if statements, leading to uncaught errors because item in if is undefined. What to do?
I see there are not many oboe tags used on SO, but please help if you can, this is more of a general javascript question about handing uncaught errors for undefined.我看到在 SO 上使用的双簧管标签并不多,但如果可以,请提供帮助,这更像是一个关于处理未定义的未捕获错误的一般 javascript 问题。 Thanks!~谢谢!~
I am using Oboe.js to stream data to webpage from a MongoDB atlas collection.我正在使用 Oboe.js 将 stream 数据从 MongoDB 地图集收集到网页。 In the MongoDB JSON, some of the _ids have a path of item.image.filename
which is a path to an image.在 MongoDB JSON 中,一些 _id 的路径为item.image.filename
,它是图像的路径。 Some do not have item.image.filename, as not all of the streamed items need an image.有些没有 item.image.filename,因为并非所有流式传输的项目都需要图像。 The loading stops when there is an undefined value and I see an error in the console.当存在未定义的值并且我在控制台中看到错误时,加载停止。
I would expect that if there is not a value present that oboe would just carry on to the next record as its default behavior.我希望如果不存在任何值,则双簧管将继续执行下一条记录作为其默认行为。 However, the _ids without the image path are throwing an exception in the if statement I am using to rule them out.但是,没有图像路径的 _id 在我用来排除它们的 if 语句中抛出异常。 I want to be clear that item.image.filename is not there with no value in it or set to null, it just doesn't exist.我想清楚 item.image.filename 不存在,其中没有任何值或设置为 null,它只是不存在。 From what little I know of MongoDB, the flexibility to have each item be unique is one of its big advantages.根据我对 MongoDB 的了解,让每个项目都独一无二的灵活性是其一大优势。
I believe I have hit on all manners of checking for this with if statements, even using a try catch block, but when compiled, the if statement itself is throwing this error: oboe-browser.min.js:5 Uncaught Error: Cannot read property 'filename' of undefined at oboe-browser.min.js:5
.我相信我已经用 if 语句进行了各种检查,即使使用了 try catch 块,但是在编译时,if 语句本身会抛出这个错误: oboe-browser.min.js:5 Uncaught Error: Cannot read property 'filename' of undefined at oboe-browser.min.js:5
。 The if else block would render html without the image if undefined, otherwise render it.如果未定义,if else 块将在没有图像的情况下渲染 html,否则渲染它。
One way to fix this would be to add a item.image.filename
to each record, but I would rather not, there are a lot of records, that's my last resort.解决这个问题的一种方法是在每条记录中添加一个item.image.filename
,但我宁愿不这样做,有很多记录,这是我最后的手段。 Seems there is a better wait to sort this out.似乎有更好的等待来解决这个问题。
Here is what I have tried:这是我尝试过的:
if(.item.image.filename)
if(item.image.filename === undefined)
if(item.image.filename == 'undefined')
if(item.image.filename == undefined)
if(item.image.filename === 'undefined')
if(item.image.filename === null)
if(item.image.filename == null)
I took an alternative route like if(item.image.filename !== undefined)
both with and without quotes, and with null too.我采用了另一种方法,例如带引号和不带引号的if(item.image.filename !== undefined)
以及 null 。
Thanks for the help!谢谢您的帮助!
You'll want to use typeof
:你会想要使用typeof
:
if(typeof item.== 'object' || typeof item.image.== 'object' || item.image;filename === undefined){ console.log("undefined as far as you're concerned"); }
What you need to know is you cannot do a === undefined
test on a variable that has not been declared.您需要知道的是,您不能对尚未声明的变量进行=== undefined
测试。 But it is okay to do that test on an Object property that doesn't exist, as long as the Object does exist.但是,只要 Object 确实存在,就可以对不存在的 Object 属性进行该测试。
try something like this:尝试这样的事情:
if(item.image.filename in this)
or或者
if(!(item.image.filename in this))
where this is an object or array这是一个 object 或数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.