我正在尝试使用带有React&Material-的表单从 firestore收集数据 中 获取并更新数据 ui。
更新useEffect函数后,没有错误发现它只是无法按我的预期工作。
attached file firestore location picture
import React, { useState, useEffect } from 'react';
// material-ui
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
// firebase
import { useFirebase } from '../../../components/FirebaseProvider';
import { useDocument } from 'react-firebase-hooks/firestore';
import AppPageLoading from '../../../components/AppPageLoading';
import { useSnackbar } from 'notistack';
function EditProduk({ match }) {
const { firestore, user } = useFirebase();
const { enqueueSnackbar } = useSnackbar();
const produkTraining = firestore.doc(`userdoc/${user.uid}/training/${match.params.trainingId}`);
const [snapshot, loading] = useDocument(produkTraining);
const [form, setForm] = useState({
name: '',
trainer: '',
price: 0,
description: ''
});
const [error, setError] = useState({
name: '',
trainer: '',
price: '',
description: ''
})
const [isSubmitting, setSubmitting] = useState(false);
useEffect(() => {
if (snapshot) {
setForm(currentForm => ({
...currentForm,
...snapshot.data()
}));
}
}, [snapshot]);
const handleChange = e => {
setForm({
...form,
[e.target.name]: e.target.value
})
setError({
...error,
[e.target.name]: ''
})
}
const validate = () => {
const newError = { ...error };
if (!form.name) {
newError.name = 'Training name must be filled';
}
if (!form.trainer) {
newError.trainer = 'trainer name must be filled';
}
if (!form.price) {
newError.price = 'price must be filled';
}
if (!form.description) {
newError.description = 'description must be filled';
}
return newError
}
const handleSubmit = async e => {
e.preventDefault();
const findErrors = validate();
if (Object.values(findErrors).some(err => err !== '')) {
setError(findErrors);
} else {
setSubmitting(true);
try {
await produkTraining.set(form, { merge: true });
enqueueSnackbar('Data has been saved', { variant: 'success' })
}
catch (e) {
enqueueSnackbar(e.message, { variant: 'error' })
}
setSubmitting(false);
}
}
if (loading) {
return <AppPageLoading />
}
return <div>
<Typography variant="h5" component="h1">Edit Training: {form.name}</Typography>
<Grid container alignItems="center" justify="center">
<Grid item xs={12} sm={6}>
<form id="produk-form" onSubmit={handleSubmit} noValidate>
<TextField
id="name"
name="name"
label="Training Name"
margin="normal"
required
fullWidth
value={form.name}
onChange={handleChange}
helperText={error.name}
error={error.name ? true : false}
disabled={isSubmitting}
/>
service cloud.firestore {
match /databases/{database}/documents {
match /userdoc/{uid} {
allow read: if request.auth.uid == uid;
allow write: if request.auth.uid == uid;
match /training/{trainingId}{
allow read, write: if request.auth.uid == uid;
}
}
}
}
请给我一些建议
答案 0 :(得分:0)
我可能是错的,但是我认为这与function EditProduk({ match })
有关。函数定义参数中的花括号可能会导致对象参数不可用,并且分配了undefined,似乎它甚至被用作数据库中文档名称的字符串。请让我知道它是否会有所帮助,因为我只是猜测。