【发布时间】:2021-03-27 19:47:28
【问题描述】:
我正在尝试使用 Firebase 在后端添加一个集合中的文档,该文档的名称是由于函数 firestoreAutoId() 而随机生成的,但是当我提交表单时,React 返回一个错误,提示“创建用户时出错 FirebaseError :使用无效数据调用函数 DocumentReference.set()。不受支持的字段值:未定义(在文档 av_deliveries/[object Object] 的字段 lastname 中找到)“
",这可能看起来很傻,但我真的不明白为什么会出现这个错误。我改变名称或值都没有关系,它会导致另一个问题。
为了更好的理解,这里是整个代码:
AddDelivery.Js
import { createDelivery, firestoreAutoId } from '../firebase';
import './AddDelivery.css';
class AddDelivery extends Component {
state = {when: '', whath: '', from: 0, to: 0, to_adress: '', to_tel: 0, name: '', to_name: '', name2: '', tel: 0, adress: '', info_comp: '', taken: false, taken_by: "X", city:''};
handleChange = (e) => {
const { name, value } = e.target;
this.setState({ [name]: value });
};
handleSubmit = async (e) => {
e.preventDefault();
const {when, whath, from, to, to_adress, to_tel, name, to_name, name2, tel, adress, info_comp, taken, taken_by, city} = this.state;
try {
let id
id = firestoreAutoId()
await createDelivery({ id }, { when }, { whath }, { from }, { to }, { to_adress }, { to_tel } , { name } , { to_name }, { name2 }, { tel } , { adress } , { info_comp } , { taken } , { taken_by } , { city });
} catch (error) {
console.log('error', error);
}
this.setState({id: '', when: '', whath: '', from: 0, to: 0, to_adress: '', to_tel: 0, name: '', to_name: '', name2: '', tel: 0, adress: '', info_comp: '', taken: false, taken_by: "X", city:''});
};
render() {
const {when, whath, from, to, to_adress, to_tel, name, to_name, name2, tel, adress, info_comp, taken, taken_by, city} = this.state;
return (
<div className="main-wrapper">
<div className="form-main-wrapper">
<p className="form-add-delivery-hero-title">Ajouter une livraison</p>
<form className="form-wrapper" onSubmit={this.handleSubmit}>
<div className="form-when-wrapper">
<div className="form-when">
<label>Quand ?</label>
<input type="date" name="when" value={when} onChange={this.handleChange} required></input>
</div>
<div className="form-when-hour">
<label>A quelle heure ?</label>
<input type="time" name="whath" value={whath} onChange={this.handleChange} required></input>
</div>
<div className="form-city-name">
<label>Ville ?</label>
<select className="form-city-selector" name="city" value={city} onChange={this.handleChange}>
<option value="Lyon">Lyon</option>
<option value="Montpellier">Montreuil</option>
<option value="Paris">Paris</option>
<option value="Vélizy-Villacoublay">Vélizy-Villacoublay</option>
<option value="Viroflay">Viroflay</option>
</select>
</div>
</div>
.... Same for the rest of the form..
Firebase.js
export const createDelivery = async (id1, when1, whath1, from1, to1, adress_to1, tel_to1, name1, name2, tel1, adress1, info_comp1, taken1, taken_by1, city1) => {
const userRef = firestore.doc(`av_deliveries/${id1}`);
const snapshot = await userRef.get();
if (!snapshot.exists) {
const { id } = id1;
const { when } = when1;
const { whath } = whath1;
const { from } = from1;
const { to } = to1;
const { adress_to } = adress_to1;
const { tel_to } = tel_to1;
const { name } = name1;
const { lastname } = name2;
const { tel } = tel1;
const { adress } = adress1;
const { info_comp } = info_comp1;
const { taken } = taken1;
const { taken_by } = taken_by1;
const { city } = city1;
try {
await userRef.set({
id,
when,
whath,
from,
to,
adress_to,
tel_to,
name,
lastname,
tel,
adress,
info_comp,
city,
});
} catch (error) {
console.log('Error in creating user', error);
}
}
};
感谢您的帮助!
【问题讨论】:
标签: javascript reactjs firebase google-cloud-firestore