给 AI 喂正确的上下文
这是你大部分杠杆所在之处。当你报告一个 bug 时,请包含:
- 逐字的错误信息。 原样复制——不要转述。转述一个错误,就像在电话里描述一张照片:你会漏掉那个最关键的细节。
- 堆栈跟踪,尤其是顶部的几行。
- 相关代码——抛出错误的函数,以及它调用的任何东西。
- 你期望的结果与实际发生的结果对比。
下面是一个强力调试提示的真实样子:
I'm getting an error when I submit the signup form. Here's the full output:
TypeError: Cannot read properties of undefined (reading 'email')
at validateUser (src/auth/validate.js:14:23)
at handleSignup (src/routes/signup.js:31:10)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
Steps to reproduce: fill in the form, click "Sign up".
Expected: a "Welcome" message.
Actual: the page goes blank and the error above appears in the console.
Here is validate.js (lines around 14):
[paste the function]
And here is the handleSignup function that calls it:
[paste the function]
What could be causing this, and how do I confirm it before changing anything?
注意最后一行。你是在请求一个假设和一个确认步骤,而不是立即修改。这能让 AI 保持诚实。
知道如何阅读你正在粘贴的那段跟踪,会很有帮助。堆栈跟踪是一份按最新到最旧排列的调用历史:最顶部那行是程序实际爆炸的地方,下面每一行都是调用了它上面那个函数的函数。所以在例子里,validateUser 是 email 从某个 undefined 的东西上被读取的地方,而 handleSignup 是调用 validateUser 的那一方。你自己代码下面的帧——比如 processTicksAndRejections——属于语言运行时;通常你可以忽略它们。从上往下读,直到碰到第一行指向你的文件,那几乎总是该开始查看的地方。