Firestore数据存储与读取失败求助(附调试日志及代码)
Hey there, let's break down what's going on here and how to fix it.
First, looking at your debug logs, the root issue isn't actually with your Firestore save/read code (that logic looks solid, by the way)—it's a authentication token fetch failure caused by a global property conflict. The error Cannot redefine property: _lat means somewhere in your project, the _lat property (likely on the global window object or Object.prototype) has been locked down (marked as non-configurable), which is clashing with Firestore's internal auth logic that tries to access or modify this property.
Here are the steps to resolve this:
1. Track down the global property conflict
- Open your browser's dev tools console and run this command to check the
_latproperty's configuration:
If the result showsObject.getOwnPropertyDescriptor(window, '_lat')configurable: false, that's the problem. Some third-party library (like older map libraries, outdated analytics tools, or even custom code) has defined this property in a way that prevents redefinition. - Audit your project's dependencies and custom scripts to find which one is setting
_latglobally. Look for code that usesObject.defineProperty(window, '_lat', ...)withconfigurable: false.
2. Ensure consistent Firebase SDK versions
Even though you upgraded to 5.0.4, mixed versions of Firebase packages (e.g., firebase/auth on 5.0.3 and firebase/firestore on 5.0.4) can cause internal conflicts.
- Check your
package.jsonto make sure all Firebase-related dependencies are on the exact same version. - Delete your
node_modulesfolder andpackage-lock.json(oryarn.lock), then runnpm install(oryarn install) to do a clean reinstall.
3. Test in a minimal environment
To isolate the issue:
- Create a new, empty project with only the Firebase Firestore SDK installed.
- Run your
savefunction there. If it works without errors, the problem is definitely coming from a conflicting dependency in your main project. Add dependencies one by one until the error reappears to pinpoint the culprit.
4. Temporary workaround (for testing)
If you need to verify your Firestore logic works while debugging the conflict, you can disable Firestore's persistence (this skips some of the internal code paths that trigger the error):
// Initialize Firebase as usual firebase.initializeApp(yourFirebaseConfig); const db = firebase.firestore(); // Disable persistence db.settings({ persistence: false });
Once you fix the global _lat property conflict, the auth token fetch will work normally, and Firestore will stop backing off (the ExponentialBackoff logs will disappear). Your original save/read code should then function as expected.
内容的提问来源于stack exchange,提问作者Felix

