英文:
Firestore duplicating uploads in a react project
问题
我在将食谱上传到我的React应用程序中的Firestore时遇到了一个令人沮丧的问题。问题是数据在Firestore集合中重复,我似乎无法找出原因。
以下是代码示例:
首先是实际的 uploadRecipesToFirestore 代码片段:
export const uploadRecipesToFirestore = async (recipes) => {
try {
const recipesCollection = firebase.firestore().collection('recipes');
const uploadPromises = recipes.map((recipe) => recipesCollection.add(recipe));
await Promise.all(uploadPromises);
console.log('Recipes uploaded to Firestore successfully!');
} catch (error) {
console.error('Error uploading recipes to Firestore:', error);
}
};
然后是 recipescontext--
import { createContext, useEffect, useState } from 'react';
import { fetchRecipesFromFirestore, uploadRecipesToFirestore } from '../utils/firebase-utils';
import PropTypes from 'prop-types';
import {recipes} from '../recipe-data';
export const RecipesContext = createContext([]);
export const RecipesProvider = ({ children }) => {
const [isUploaded, setIsUploaded] = useState(false);
const [fetchedRecipes, setFetchedRecipes] = useState([]);
useEffect(() => {
if (!isUploaded) {
uploadRecipesToFirestore(recipes)
.then(() => {
console.log('Recipes uploaded to Firestore successfully!');
setIsUploaded(true);
})
.catch((error) => {
console.error('Error uploading recipes to Firestore:', error);
});
}
}, [isUploaded]);
useEffect(() => {
const fetchRecipes = async () => {
const recipes = await fetchRecipesFromFirestore();
setFetchedRecipes(recipes);
};
fetchRecipes();
}, []);
return (
<RecipesContext.Provider value={fetchedRecipes}>{children}</RecipesContext.Provider>
);
};
RecipesProvider.propTypes = {
children: PropTypes.node.isRequired
};
非常感谢您对可能导致此重复问题的任何见解或建议。如果您需要任何额外的信息,请告诉我。提前感谢您的帮助!
我想我应该提一下,当我安装firebase时,我遇到了一些漏洞,这是错误:
rotobufjs Prototype Pollution vulnerability - https://github.com/advisories/GHSA-h755-8qp9-cq85
修复方法是安装firebase的先前版本,但它被标记为破坏性变更。我是一名没有经验的新开发人员,不确定是否是正确的选择。我也不确定这是否可能导致问题,或者是否有关。我的计划是等待几天,直到可能有一个npm审计修复,不触发破坏性更改。这也是我第一次使用Vite而不是create-react-app,我不得不学习一些新的东西,create-react-app不要求的。
这是我尝试过的:
-
我有一个
RecipesProvider
组件,包装在我的应用程序中的Home
和RecipeShowcase
组件周围。在RecipesProvider
中,我使用uploadRecipesToFirestore
函数将食谱数据上传到Firestore。 -
我检查了对
uploadRecipesToFirestore
函数的重复调用,但在我的代码中只有一个出现。因此,我认为不是无意中多次调用的问题。 -
我还尝试将上传和获取逻辑分成两个单独的
useEffect
钩子,放在RecipesProvider
组件中。我认为这可能会解决问题,但不幸的是,数据仍然重复。 -
我已经检查了Firestore配置、项目设置和Firebase项目设置,但一切似乎都井井有条。我没有看到任何明显的问题。
-
浏览器控制台没有显示任何错误,但有一个关于遇到重复键的警告。我认为这仅仅是因为数据本身重复导致的。
英文:
I'm running into a frustrating issue while uploading recipes to Firestore in my React application. The problem is that the data is being duplicated in the Firestore collection, and I can't seem to figure out why.
Here's how the code looks:
first the actual uploadRecipesToFirestore snippet:
export const uploadRecipesToFirestore = async (recipes) => {
try {
const recipesCollection = firebase.firestore().collection('recipes');
const uploadPromises = recipes.map((recipe) => recipesCollection.add(recipe));
await Promise.all(uploadPromises);
console.log('Recipes uploaded to Firestore successfully!');
} catch (error) {
console.error('Error uploading recipes to Firestore:', error);
}
};
then the recipescontext--
import { createContext, useEffect, useState } from 'react';
import { fetchRecipesFromFirestore, uploadRecipesToFirestore } from '../utils/firebase-utils';
import PropTypes from 'prop-types';
import {recipes} from '../recipe-data'
export const RecipesContext = createContext([]);
export const RecipesProvider = ({ children }) => {
const [isUploaded, setIsUploaded] = useState(false);
const [fetchedRecipes, setFetchedRecipes] = useState([]);
useEffect(() => {
if (!isUploaded) {
uploadRecipesToFirestore(recipes)
.then(() => {
console.log('Recipes uploaded to Firestore successfully!');
setIsUploaded(true);
})
.catch((error) => {
console.error('Error uploading recipes to Firestore:', error);
});
}
}, [isUploaded]);
useEffect(() => {
const fetchRecipes = async () => {
const recipes = await fetchRecipesFromFirestore();
setFetchedRecipes(recipes);
};
fetchRecipes();
}, []);
return (
<RecipesContext.Provider value={fetchedRecipes}>{children}</RecipesContext.Provider>
);
};
RecipesProvider.propTypes = {
children: PropTypes.node.isRequired
};
I'd really appreciate any insights or suggestions on what might be causing this duplication issue. If you need any additional information, please let me know. Thanks in advance for your help!
I guess I should mention I'm getting some vulnerabilities when installing firebase, this is the error:
rotobufjs Prototype Pollution vulnerability - https://github.com/advisories/GHSA-h755-8qp9-cq85
The fix is installing a previous version of firebase but it is labelled as a breaking change. I'm a new developer with no experience with this and I'm not sure if it's the right call. I am also not sure if that might be causing the issue or if it's at all related. My plan was to wait for a few days until maybe there was a npm audit fix that didn't trigger the breaking changes. This is also my first time using Vite instead of create-react-app, and I had to learn some new things that the create-react-app didn't demand.
This is what I've tried
-
I have a
RecipesProvider
component that wraps around theHome
andRecipeShowcase
components in my app. Inside theRecipesProvider
, I use theuploadRecipesToFirestore
function to upload the recipe data to Firestore. -
I've checked for duplicate calls to the
uploadRecipesToFirestore
function, but there is only one occurrence in my code. So, I don't think unintentional multiple calls are the issue. -
I also tried separating the uploading and fetching logic into two separate
useEffect
hooks within theRecipesProvider
component. I thought this might solve the problem, but unfortunately, the data is still being duplicated. -
I've reviewed the Firestore configuration, project settings, and Firebase project setup, but everything seems to be in order. I don't see any obvious issues there.
-
The browser console doesn't show any errors, but there is a warning about encountering duplicate keys. I believe this is only happening because the data itself is being duplicated.
答案1
得分: 1
这可能是两个 useEffect
钩子同时运行引起的。为了防止重复,我们可以修改 useEffect
如下:
useEffect(() => {
uploadRecipesToFirestore(recipes)
.then(() => {
console.log('Recipes uploaded to Firestore successfully!');
setIsUploaded(true);
})
.catch((error) => {
console.error('Error uploading recipes to Firestore:', error);
});
}, []);
另外,您提到了Firebase的安全问题。为了解决这个问题,在对 useEffect
进行任何更改之前,将Firebase更新到一个没有任何破坏性变化的安全版本。
英文:
This might be caused by two useEffect
hooks running at the same time. To prevent duplicates, we can modify the useEffect
like this:
useEffect(() => {
uploadRecipesToFirestore(recipes)
.then(() => {
console.log('Recipes uploaded to Firestore successfully!');
setIsUploaded(true);
})
.catch((error) => {
console.error('Error uploading recipes to Firestore:', error);
});
}, []);
Additionally, you mentioned a security issue with Firebase. To fix that, update Firebase to a secure version without any breaking changes before making changes to the useEffect
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论