'how to handle Uncaught (in promise) TypeError: Cannot read properties of undefined (reading
Solution 1:[1]
The 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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | t.niese |

