Function updateDoc() called with invalid data. Nested arrays are not supported (found in document)

huangapple go评论82阅读模式
英文:

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 &#39;firebase/firestore&#39;;
import React, { useState } from &#39;react&#39;
import { useParams } from &#39;react-router-dom&#39;;
import EditNoteIcon from &#39;@mui/icons-material/EditNote&#39;;
import { db } from &#39;../../firebase&#39;;
const EditInvngs = ({id, name, date, doctor, status}) =&gt; {
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) =&gt; {
e.preventDefault()
try {
const taskDocument = doc(db, &quot;dentist/&quot; + userId , &quot;investigations&quot;, id);
await updateDoc(taskDocument, {
name: createName,
doctor: createDoctor,
date: createDate,
status: createStatus,
});
window.location.reload();
} catch (err) {
console.log(err);
}
}
return (
&lt;&gt;
{/* Modal Button */}
&lt;button
type=&quot;button&quot;
className=&quot;btn btn-primary btn-sm&quot;
data-bs-toggle=&quot;modal&quot;
data-bs-target={`#id${id}`}&gt;
&lt;EditNoteIcon/&gt;
&lt;/button&gt;
&lt;div
className=&quot;modal fade&quot;
id={`id${id}`}
tabIndex=&quot;-1&quot;
aria-labelledby=&quot;editLabel&quot;
aria-hidden=&quot;true&quot;&gt;
&lt;div className=&quot;modal-dialog&quot;&gt;
&lt;div className=&quot;modal-content&quot;&gt;
&lt;div className=&quot;modal-header&quot;&gt;
&lt;h5 className=&quot;modal-title&quot; id=&quot;editLabel&quot; style={{color:&quot;grey&quot;}}&gt;
Update Investigations&lt;/h5&gt;
&lt;button type=&quot;button&quot; className=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&quot;modal-body&quot;&gt;
&lt;input
type=&quot;text&quot;
className=&quot;form-control&quot;
placeholder=&quot;enter investigation here...&quot;
defaultValue={createName}
onChange={(e) =&gt; setCreateName(e.target.value)}
size=&quot;3080&quot;
/&gt;
&lt;input
type=&quot;date&quot;
className=&quot;form-control&quot;
defaultValue={createDate}
onChange={(e) =&gt; setCreateDate(e.target.value)}
/&gt;
&lt;input
type=&quot;text&quot;
placeholder=&quot;Consultant doctor..&quot;
className=&quot;form-control&quot;
defaultValue={createDoctor}
onChange={(e) =&gt; setCreateDoctor(e.target.value)}
size=&quot;3080&quot;
/&gt;
&lt;input
type=&quot;text&quot;
placeholder=&quot;enter status here..&quot;
className=&quot;form-control&quot;
defaultValue={createStatus}
onChange={(e) =&gt; setCreateStatus(e.target.value)}
size=&quot;3080&quot;
/&gt;
&lt;/div&gt;
&lt;div className=&quot;modal-footer&quot;&gt;
&lt;button type=&quot;button&quot; className=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
&lt;button
type=&quot;button&quot;
className=&quot;btn btn-primary&quot;
onClick={e =&gt; updateData(e)}
&gt;Update Investigation&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/&gt;
)
}
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) =&gt; {
e.preventDefault()
try {
const taskDocument = doc(db, &quot;dentist/&quot; + userId , &quot;investigations&quot;, id);
await updateDoc(taskDocument, {
name: createName,
});
window.location.reload();
} catch (err) {
console.log(err);
}
}

Function updateDoc() called with invalid data. Nested arrays are not supported (found in document)

答案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

huangapple
  • 本文由 发表于 2023年6月6日 06:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定