admin管理员组文章数量:1392101
I want to handle Uncaught (in promise):
TypeError: Cannot read properties of undefined (reading) this error in javascript. I want to alert if this type of error occurs....
How to do this in javascirpt ??
if(typeof( dataName) === undefined)
{
alert("undefined")
}
I want to handle Uncaught (in promise):
TypeError: Cannot read properties of undefined (reading) this error in javascript. I want to alert if this type of error occurs....
How to do this in javascirpt ??
if(typeof( dataName) === undefined)
{
alert("undefined")
}
Share
Improve this question
edited Mar 10, 2022 at 9:40
t.niese
40.9k9 gold badges78 silver badges109 bronze badges
asked Mar 10, 2022 at 8:47
user17729370user17729370
4
- put some code and ss of output – Nilesh Mishra Commented Mar 10, 2022 at 8:51
- 1 did you want to handle the error (.catch), or find the cause of the error (post some code), or prevent the error (check your variables)? – Bravo Commented Mar 10, 2022 at 8:55
- Does the posted code relate to the question? – traktor Commented Mar 10, 2022 at 9:25
-
1
typeof
returns a string and notundefined
so it is eitherdataName === undefined
ortypeof( dataName) === 'undefined')
to check ifdataName
isundefined
. – t.niese Commented Mar 10, 2022 at 9:41
1 Answer
Reset to default 1The error indicates that your getProduct
function is async
and that you try to access the property english_name
for a variable that is undefined
.
So if you want to handle that error you need to handle the error case for the promise chain at some point. Or ensure that the error does not happen at all:
async function getProduct() {
let dataName = undefined;
dataName.english_name = 1;
}
getProduct().catch(err => {
console.error('error occured: ',err.message)
});
or
async function getProduct() {
let dataName = undefined;
dataName.english_name = 1;
}
async function run() {
try {
await getProduct();
} catch (err) {
console.error('error occured: ', err.message)
}
}
run();
typeof
returns a string and not undefined
so it is either dataName === undefined
or typeof( dataName) === 'undefined')
to check if dataName
is undefined
:
async function getProduct() {
let dataName = undefined;
if (typeof(dataName) !== 'undefined') {
dataName.english_name = 1;
}
if (dataName !== undefined) {
dataName.english_name = 1;
}
}
async function run() {
try {
await getProduct();
} catch (err) {
console.error('error occured: ', err.message)
}
}
run();
本文标签:
版权声明:本文标题:javascript - how to handle Uncaught (in promise) TypeError: Cannot read properties of undefined (reading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744775717a2624610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论