【发布时间】:2020-03-22 18:49:16
【问题描述】:
我还是新手,还在使用 react 和 firebase 进行自学,请帮我处理这段代码。
我尝试使用带有 react 和 material-ui 的表单从 firestore 收集数据 获取和更新数据 ,
在我更新 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}
/>
firestore 规则
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;
}
}
}
}
请给我一些建议
【问题讨论】:
-
@AtinSingh 我不确定这个。签出此代码沙箱codesandbox.io/s/…
标签: reactjs firebase google-cloud-firestore firebase-security