解读 JavaScript 日志报错,通常可以分**“看哪里 → 看什么 → 怎么查 → 怎么修”**四个步骤来做。下面用一套通用方法,配合常见示例说明。
app.js:23)error.log)一个典型 JS 报错通常包含:
TypeError: Cannot read properties of undefined (reading 'name')
at getUser (app.js:23:15)
at main (app.js:30:5)
常见类型:
TypeError:类型不对(最常见)ReferenceError:变量未定义SyntaxError:语法写错RangeError:数值越界Promise / Async 相关:未捕获异常Cannot read properties of undefined (reading ‘name’)
意思是:
undefined.nameat getUser (app.js:23)
at main (app.js:30)
说明:
getUsermain 调用它时出的问题const a = 123;
a(); // TypeError
✅ 原因:把非函数当函数调用
const user = undefined;
console.log(user.name);
✅ 原因:对象没拿到(接口慢 / 写错字段)
✅ 修复:
user?.name
console.log(a); // a 没声明
✅ 原因:
if (true) {
console.log('hi')
✅ 原因:少括号 / 少分号 / JSON 格式错
fetch('/api').then(res => res.json())
✅ 原因:Promise 没 catch
✅ 修复:
.catch(err => console.error(err))
debugger;
try {
...
} catch (e) {
console.error(e.stack);
}
你可以直接贴:
我可以逐行帮你翻译并报错原因 + 修复方案。