更新后返回422状态码时出现Cannot read property 'hasOwnProperty'报错求助
Let's break this down and fix the issue step by step.
First, looking at your error stack trace: the problem happens in the validateResponseFormat function at line 147058. The code is trying to call hasOwnProperty on a variable that’s undefined—meaning it assumed a value existed (and was an object) when it didn’t, specifically when your API returns a 422 status code.
Connecting this to your API response
Your 422 error response has this structure:
{ "error": { "details": [{ "status": "week", "message": "", "target": "ModelState" } ], "innerError": null, "status": "422", "message": "Invalid model", "target": "ModelState" } }
Chances are, your validateResponseFormat function is built to handle successful responses (like 200 status codes) which likely follow a different structure—maybe something like { data: ... }. When a 422 error comes in, the function still tries to access a property that exists in success responses (e.g., response.data) but doesn’t exist in your error response. Since that property is undefined, calling hasOwnProperty on it throws the TypeError you’re seeing.
Step-by-step fixes
Inspect the
validateResponseFormatfunction
Jump to line 147058 in your source code (or the unminified version of0.chunk.js) and find which variable is being checked withhasOwnProperty. It might look like this:// Problematic code if (responseData.hasOwnProperty('someField')) { // ... validation logic }Here,
responseDatais undefined when handling the 422 error.Add a guard clause for undefined values
Update the code to verify the variable exists before callinghasOwnProperty:// Fixed code if (responseData && responseData.hasOwnProperty('someField')) { // ... validation logic }This ensures you only run the check if the variable is actually an object.
Handle error responses explicitly
Since your API returns anerrorobject for 422 statuses, adjust your validation logic to account for this case:function validateResponseFormat(response) { // First check if it's an error response if (response.error) { // Add validation for error structure (if needed) return; } // Proceed with success response validation if (response.data && response.data.hasOwnProperty('someField')) { // ... } }Check your response interceptor (if using one)
If you’re using a library like Axios with interceptors, make sure it isn’t transforming the 422 response in a way that strips or mangles the error data—this could lead to undefined values being passed to the validation function.
Quick test to confirm
Temporarily log the input to validateResponseFormat when a 422 is returned. This will show you exactly what data the function is receiving, which should confirm that the variable in question is undefined.
内容的提问来源于stack exchange,提问作者sorcer1

