英文:
Function updateDoc() called with invalid data. Nested arrays are not supported (found in document)
问题
以下是您提供的代码的翻译部分:
我在我的Firestore中有一个子集合,我正在尝试更新模型中的数据。我注意到,如果我添加多个状态以进行更新,就会出现“调用了无效数据的updateDoc()函数”...
这是我的代码片段:-
import { doc, updateDoc } from 'firebase/firestore';
import React, { useState } from 'react'
import { useParams } from 'react-router-dom';
import EditNoteIcon from '@mui/icons-material/EditNote';
import { db } from '../../firebase';
const EditInvngs = ({id, name, date, doctor, status}) => {
const [createDate, setCreateDate] = useState([date])
const [createName, setCreateName] = useState([name])
const [createDoctor, setCreateDoctor] = useState([doctor])
const [createStatus, setCreateStatus] = useState([status])
const {userId} = useParams();
console.log(userId)
const updateData = async (e) => {
e.preventDefault()
try {
const taskDocument = doc(db, "dentist/" + userId , "investigations", id);
await updateDoc(taskDocument, {
name: createName,
doctor: createDoctor,
date: createDate,
status: createStatus,
});
window.location.reload();
} catch (err) {
console.log(err);
}
}
return (
<>
{/* Modal Button */}
<button
type="button"
className="btn btn-primary btn-sm"
data-bs-toggle="modal"
data-bs-target={`#id${id}`}>
<EditNoteIcon/>
</button>
<div
className="modal fade"
id={`id${id}`}
tabIndex="-1"
aria-labelledby="editLabel"
aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="editLabel" style={{color:"grey"}}>
更新调查</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body">
<input
type="text"
className="form-control"
placeholder="在此输入调查..."
defaultValue={createName}
onChange={(e) => setCreateName(e.target.value)}
size="3080"
/>
<input
type="date"
className="form-control"
defaultValue={createDate}
onChange={(e) => setCreateDate(e.target.value)}
/>
<input
type="text"
placeholder="顾问医生.."
className="form-control"
defaultValue={createDoctor}
onChange={(e) => setCreateDoctor(e.target.value)}
size="3080"
/>
<input
type="text"
placeholder="在此输入状态.."
className="form-control"
defaultValue={createStatus}
onChange={(e) => setCreateStatus(e.target.value)}
size="3080"
/>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
<button
type="button"
className="btn btn-primary"
onClick={e => updateData(e)}
>更新调查</button>
</div>
</div>
</div>
</div>
</>
)
}
export default EditInvngs
当我尝试只在updateDoc中使用一个状态时,例如,只使用“name”进行更新时,它完美地工作,但一旦我添加多个状态,它就会抛出错误...
const updateData = async (e) => {
e.preventDefault()
try {
const taskDocument = doc(db, "dentist/" + userId , "investigations", id);
await updateDoc(taskDocument, {
name: createName,
});
window.location.reload();
} catch (err) {
console.log(err);
}
}
希望这有助于您理解代码的翻译和问题的描述。
英文:
I have a subcollection in my firestore and I'm trying to update the data in the modal. I noticed if I add more than one state to update, I get the "Function updateDoc() called with invalid data"...
Here's my code snippet:-
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { doc, updateDoc } from 'firebase/firestore';
import React, { useState } from 'react'
import { useParams } from 'react-router-dom';
import EditNoteIcon from '@mui/icons-material/EditNote';
import { db } from '../../firebase';
const EditInvngs = ({id, name, date, doctor, status}) => {
const [createDate, setCreateDate] = useState([date])
const [createName, setCreateName] = useState([name])
const [createDoctor, setCreateDoctor] = useState([doctor])
const [createStatus, setCreateStatus] = useState([status])
const {userId} = useParams();
console.log(userId)
const updateData = async (e) => {
e.preventDefault()
try {
const taskDocument = doc(db, "dentist/" + userId , "investigations", id);
await updateDoc(taskDocument, {
name: createName,
doctor: createDoctor,
date: createDate,
status: createStatus,
});
window.location.reload();
} catch (err) {
console.log(err);
}
}
return (
<>
{/* Modal Button */}
<button
type="button"
className="btn btn-primary btn-sm"
data-bs-toggle="modal"
data-bs-target={`#id${id}`}>
<EditNoteIcon/>
</button>
<div
className="modal fade"
id={`id${id}`}
tabIndex="-1"
aria-labelledby="editLabel"
aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="editLabel" style={{color:"grey"}}>
Update Investigations</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body">
<input
type="text"
className="form-control"
placeholder="enter investigation here..."
defaultValue={createName}
onChange={(e) => setCreateName(e.target.value)}
size="3080"
/>
<input
type="date"
className="form-control"
defaultValue={createDate}
onChange={(e) => setCreateDate(e.target.value)}
/>
<input
type="text"
placeholder="Consultant doctor.."
className="form-control"
defaultValue={createDoctor}
onChange={(e) => setCreateDoctor(e.target.value)}
size="3080"
/>
<input
type="text"
placeholder="enter status here.."
className="form-control"
defaultValue={createStatus}
onChange={(e) => setCreateStatus(e.target.value)}
size="3080"
/>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button
type="button"
className="btn btn-primary"
onClick={e => updateData(e)}
>Update Investigation</button>
</div>
</div>
</div>
</div>
</>
)
}
export default EditInvngs
<!-- end snippet -->
When I tried to use only one state in the updateDoc, for instance, using only "name" as an update it works perfectly but the moment I add more than one state it throws the error..
Here's what I mean...
const updateData = async (e) => {
e.preventDefault()
try {
const taskDocument = doc(db, "dentist/" + userId , "investigations", id);
await updateDoc(taskDocument, {
name: createName,
});
window.location.reload();
} catch (err) {
console.log(err);
}
}
答案1
得分: 1
useState()
中的 [ ]
会返回数组值,而 Firestore 不支持嵌套数组作为原始类型。因此,删除 [ ]
将解决问题。所以修改你的代码片段如下:
const [createDate, setCreateDate] = useState(date);
const [createName, setCreateName] = useState(name);
const [createDoctor, setCreateDoctor] = useState(doctor);
const [createStatus, setCreateStatus] = useState(status);
参考链接:https://react.dev/reference/react/useState#usestate
英文:
useState()
with braces [ ]
will return array values, and firestore does not have nested arrays as primitive types. Hence removing [ ]
will solve the problem. So modify your snippet as below
const [createDate, setCreateDate] = useState(date);
const [createName, setCreateName] = useState(name);
const [createDoctor, setCreateDoctor] = useState(doctor);
const [createStatus, setCreateStatus] = useState(status);
Reference: https://react.dev/reference/react/useState#usestate
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论