我有一个大学项目,使用React Native创建一个应用程序,我们必须使用firebase来制作数据库并在hte页面中使用它,我有一个问题,我有一个页面可以查看产品,这个页面有一个按钮,按下后它会更改页面中的产品并显示下一个产品,我在页面顶部有 5 个按钮来分配给不同的表,我基本上必须访问集合子集合的特定字段但每次我按下按钮时它都会在标题中返回错误:
代码:
const [trocar, setTrocar] = useState(0);
const [selectedTable, setSelectedTable] = useState(1);
const [ldata, setLData] = useState({});
const incrementRecipeTab = async (trocar, selectedTable) => {
try {
// Determine the subcollection path based on the selected table
const subcollectionName = `table${selectedTable}`;
// Document reference for the specific recipe
const recipeDocPath = `table/${subcollectionName}/recipe${trocar}`;
const recipeDocRef = doc(db, recipeDocPath);
// Log the path to debug
console.log(`Document path: ${recipeDocPath}`);
// Get the document snapshot
const recipeDocSnapshot = await getDoc(recipeDocRef);
if (recipeDocSnapshot.exists()) {
// Increment the specific recipe field
await updateDoc(recipeDocRef, {
[`recipe${trocar}`]: increment(1) // Increment dynamically
});
console.log(`Recipe ${trocar} incremented successfully for table ${selectedTable}`);
} else {
console.error(`Recipe document does not exist for table ${selectedTable} at path ${recipeDocPath}`);
}
} catch (ex) {
console.error('Error incrementing recipe:', ex);
}
};
const handleAddPress = () => {
// Adicionar logs para debug
console.log('Current Recipe:', trocar);
console.log('Table ID:', selectedTable);
// Validar os valores antes de chamar a função
if (trocar != null && selectedTable != null) {
incrementRecipeTab(trocar, selectedTable);
} else {
console.error('trocar or selectedTable is not defined');
}
};
const renderItem = (item) => (
<View>
{item && (
<View style={[styles.container, { marginTop: 8 + rowGap }]}>
<View style={styles.column}>
<Image source={item.image} style={styles.imagPrato} />
<View style={styles.textBoxContainer}>
<Text style={styles.boxText}>{item.name}</Text>
<Text style={styles.boxTextTemp}>{item.time} Minutes</Text>
<View style={styles.imageVectorContainer}>
<Image source={require("../assets/Vector.png")} style={styles.imageVector} />
</View>
<View style={styles.rectangle}>
<Text style={[styles.boxText, { color: 'white', top: '31%', position: 'relative' }]}>Recipe:</Text>
</View>
<View style={[styles.rectangle, { left: '55%' }]}>
<Text style={[styles.boxText, { color: 'white', top: '31%', position: 'relative' }]}>Price: {item.price} €</Text>
</View>
<View style={styles.textboxContainerIngre}>
<Text>{item.recipe}</Text>
</View>
<View style={styles.rectangleCenter}>
<Text style={[styles.boxText, { color: 'white', top: '31%', position: 'relative' }]}>Preparation:</Text>
</View>
<View style={[styles.textboxContainerIngre, { top: '160%' }]}>
<Text>{item.preparation}</Text>
</View>
<TouchableOpacity onPress={() => handleAddPress()} style={styles.rectangleadd}>
<Text style={[styles.boxText, { color: 'white', top: '31%', position: 'relative' }]}>Add: {item.quant}</Text>
</TouchableOpacity>
</View>
<Button onPress={HandlePress} title="Next" />
</View>
</View>
)}
</View>
); `
如果有人想要更多部分代码,请询问
我想增加配方[trocar],trocar是页面中显示的指定可选表中的当前产品,我似乎不明白为什么它显示该错误
我认为您误解了 Firestore 术语。 以下是您如何描述您在屏幕截图中看到的每个组件:
const docRef = doc(db, "table/table2");
const snapshot = await getDoc(docRef);
const data = snapshot.data(); // this is an object whose entries are document fields
console.log(snapshot.data()); // this will print all of the fields in the doc
然后,您可以使用正确的引用更新文档中的字段:
await updateDoc(docRef, {
[`recipe${trocar}`]: increment(1)
});