【问题标题】:REACT +FIREBASE db._checkNotDeleted is not a function on UPDATEREACT +FIREBASE db._checkNotDeleted 不是 UPDATE 上的函数
【发布时间】:2022-02-17 23:27:35
【问题描述】:

我正在尝试从 firebase 实时数据库中更新数据。

这是一个仓库管理应用程序,用户最初使用一些数据创建一个对象,然后当仓库发生某些事情时可能需要更改数据而不创建新对象。

现在我正在使用 get() 函数从服务器获取数据到特定 ID。

然后我显示数据并允许用户根据需要更改一些字段。

最后我想更新服务器中的数据。

import { getDatabase, ref, get, child, update} from "firebase/database";


const UpdateItem = () => {
  const [data, setData]=useState({
    numero: 0,
    date: '' ,
    nome: "",
   battistrada:'',
    cognome: "",
    nomeAzienda: "",
    modello: "",
    targa: "",
    telefono: "",
    mail: "",
    quantità: '',
    misura: "",
    marca: "",
    cerchi: "",
  })
  const [searchParams,setSearchParams]=useSearchParams()
  const [success, setSuccess] = useState(false);
  const dbRef =ref(getDatabase());

  const dateFormat = "DD/MM/YYYY";


  //get current data
  useEffect(() =>get(child(dbRef,'lavorazione/' + searchParams.get('id'))).then((snapshot)=>{
    if(snapshot.exists()){
        setData(snapshot.val())
        console.log('[CONVERTED-DATA]',snapshot.val(), '[stato]', data);
    }else{
        console.log('[GET] NO data available')
    }
}).catch((error)=>{
    console.log(error)
})
 , [])

//update data
 const updateHandler=()=>{
  update(ref(dbRef, 'lavorazione/' + searchParams.get('id')), data)
  setSuccess(true); 
  console.log("[PRE-TIMEOUT", success);
  setTimeout(() => {
    setSuccess(false);
    console.log("[TIMEOUT]", success);
  }, 5000);
}

//... handlers to update the state

 return (
    <Form
      form={form}
      layout="vertical"
      onFinish={updateHandler}
      autoComplete="off"
    >
      <Row>
        <Col lg={12} xs={24} sm={24} className="form-container">
          <h2>CODICE LAVORAZIONE</h2>
         <Space direction="vertical" size='large'>
         <span style={{fontSize:'32px'}}>{data.numero}</span>
         <DatePicker
              onChange={onChangeDate}
              format={dateFormat}
              placeholder={data.date}
              name="date"
             
       
            />
          </Space>
          <h2>ANAGRAFICA CLIENTE</h2>
          <Form.Item
            name="nome"
            label="Nome"
            rules={[ { type: "string", min: 3 }]}
            initialValue={data.nome}

          >
            <Input
              placeholder={data.nome}
              value={data.nome}
              name="nome"
              onChange={onChange}
            />
          </Form.Item>
          <Form.Item
            name="cognome"
            label="Cognome"
            rules={[ { type: "string", min: 3 }]}
            initialValue={data.cognome}
          >
            <Input
              placeholder={data.cognome}
              value={data.cognome}
              name="cognome"
              onChange={onChange}
            />
          </Form.Item>
          
          //other inputs down there

根据 firebase 文档,这应该可以正常工作,但实际上我得到 TypeError: db._checkNotDeleted is not a function

我使用 ref 的方式有问题吗?

提前谢谢你

【问题讨论】:

  • 错误来自哪一行?

标签: javascript reactjs firebase firebase-realtime-database


【解决方案1】:
update(ref(dbRef, 'lavorazione/' + searchParams.get('id')), data)

应该是

update(dbRef, data)

数据的格式应该是{ path: value }

例如

{
    'lavorazione/1234': 'somevalue'
}

多次更新示例

const updates = {}

update[`lavorazione/${searchParams.get('id1')}`] = 'some value'
update[`lavorazione/${searchParams.get('id2')}`] = 'some value for id2'
update[`lavorazione/${searchParams.get('id3')}`] = 'some value for id3'

update(dbRef, updates)

https://firebase.google.com/docs/database/web/read-and-write#updating_or_deleting_data

【讨论】:

  • 谢谢!那实际工作,但如果我想将值更新为特定 ID 怎么办?那是我的数据库结构: app-gommista-default-rtdb: +lavorazione 1{name,..} 2{name,..} 3{name,..} ... 以此类推,queryParams 我达到了一个特定的 ID,但是我应该如何将它传递给更新函数?
  • 如果您查看 firebase google docs 中的示例,它会向您展示如何做到这一点。我添加了一个简单的例子
猜你喜欢
  • 2022-01-14
  • 2022-01-21
  • 1970-01-01
  • 2018-11-30
  • 2020-02-19
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2022-07-23
相关资源
最近更新 更多