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. Thanks!~
I am using Oboe.js to stream data to webpage from a MongoDB atlas collection. In the MongoDB JSON, some of the _ids have a path of item.image.filename
which is a path to an image. Some do not have item.image.filename, as not all of the streamed items need an image. 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. 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. From what little I know of MongoDB, the flexibility to have each item be unique is one of its big advantages.
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
. The if else block would render html without the image if undefined, otherwise render it.
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. 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.
Thanks for the help!
You'll want to use
typeof
:What you need to know is you cannot do a
=== undefined
test on a variable that has not been declared. But it is okay to do that test on an Object property that doesn't exist, as long as the Object does exist.