【问题标题】:I want edit data in firebase database?我想在 firebase 数据库中编辑数据?
【发布时间】:2022-11-12 12:46:02
【问题描述】:

我想编辑 firebase 数据库中的数据。

但是根据我的语法,我没有得到正确的答案。你能帮助我吗。

 const addNewData = async (e) => {
  e.preventDefault();

  let data = {
  sifra:sifraRef.current.value,
  naziv:nazivRef.current.value,
  detalji_dijete:detaljiRef.current.value,
  opis:opisRef.current.value,
  broj_obroka:brojObrokaRef.current.value,
  napomena:napomenaRef.current.value
}  
const uuid = uid();
await updateDoc(collection(db, `namirnice/${uuid}`), data)


  close();

  } 

我改变了我的语法,但我在控制台中得到了这个答案。 顺便说一句,所有这些方式都来自 youtube 教程。

但是我现在的 console.log 是:Uncaught (in promise) FirebaseError: Invalid collection reference。集合引用必须有奇数个段,但 namirnice/5b61b327e2e 有 2 个。

【问题讨论】:

  • collection(db..) 应该是 updateDoc() 中的 doc(db, ..) 您能否编辑您的问题并将代码添加为文本,以便我们更轻松地复制和编辑它?
  • 我更改为 doc,但现在我从控制台收到此消息。未捕获(承诺)FirebaseError:无效的集合引用。集合引用必须有奇数个段,但 namirnice/f79db22da55 有 2 个。编辑:这与上一篇文章中的数据库相同
  • await addDoc(collection(db, 'namirnice'), data)

标签: javascript firebase google-cloud-firestore


【解决方案1】:

你可以试试updateDoc
Firestore updateDoc documentation
Example

仅供参考,如果您不知道,集合和文档是不同的。参考差异:

const alovelaceDocumentRef = doc(db, 'users', 'alovelace'); //even number of segments
const usersCollectionRef = collection(db, 'users'); //odd number of segments

Firestore data model documentation 中讨论的其他差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多