简体   繁体   English

未捕获的类型错误:firebase.firestore()…set 不是 HTMLButtonElement 的 function。<anonymous></anonymous>

[英]Uncaught TypeError: firebase.firestore()…set is not a function at HTMLButtonElement.<anonymous>

I use Firebase to develop a web app.我使用 Firebase 来开发 web 应用程序。 I got this error message我收到此错误消息

Uncaught TypeError: firebase.firestore(...).collection(...).orderBy(...).limit(...).set is not a function
    at HTMLButtonElement.<anonymous> (main.js:152)

because of this code.因为这个代码。

firebase.firestore().collection('script').orderBy('timestamp','desc').limit(1).set({
    finaltext: resultText.value,
    name: firebase.auth().currentUser.displayName,
    uid: firebase.auth().currentUser.uid
  },{merge: true}).catch(function(error){
    console.error('Error writing new message to Firestore', error);
  });

I searched the Internet, but couldn't find why this error message occurred.我搜索了互联网,但找不到出现此错误消息的原因。 Could you give me any advice, please?请给我一些建议好吗?

The method limit returns a value of type Query :方法limit返回Query类型的值:

limit限制

limit ( limit: number ): Query < T >

Creates and returns a new Query that only returns the first matching documents.创建并返回一个只返回第一个匹配文档的新查询。

https://firebase.google.com/docs/reference/js/firebase.firestore.Query#limit https://firebase.google.com/docs/reference/js/firebase.firestore.Query#limit

Therefore both orderBy and limit are used when you want to retrieve data.因此,当您要检索数据时,会同时使用orderBylimit If you want to add data to the database then just do the following:如果要向数据库添加数据,只需执行以下操作:

firebase.firestore().collection('script').set({
    finaltext: resultText.value,
    name: firebase.auth().currentUser.displayName,
    uid: firebase.auth().currentUser.uid
  },{merge: true}).catch(function(error){
    console.error('Error writing new message to Firestore', error);
  });

You need to first fetch the desired document, with get() , before writing the new field(s) with set() (with merge option) or update() .在使用set() (使用合并选项)或update()编写新字段之前,您需要先使用get()获取所需的文档。

firebase.firestore().collection('script').orderBy('timestamp','desc').limit(1).get()
.then(querySnapshot => {
    if (!querySnapshot.empty) {
        //We are sure the document exists, we can then use update()
        return querySnapshot.docs[0].ref.update({
          finaltext: resultText.value,
          name: firebase.auth().currentUser.displayName,
          uid: firebase.auth().currentUser.uid
        });
    } else {
       throw new Error("No document");
    }
})
.catch(error => {
    console.log("Error:", error);
});

Note that the get() method returns a QuerySnapshot , so you need to do querySnapshot.docs[0] to get the DocumentSnapshot corresponding to the unique doc returned by the query.请注意, get()方法返回一个QuerySnapshot ,因此您需要执行querySnapshot.docs[0]以获取与查询返回的唯一DocumentSnapshot对应的 DocumentSnapshot。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:$(…).modal在HTMLButtonElement中不是函数。 <anonymous> - Uncaught TypeError: $(…).modal is not a function at HTMLButtonElement.<anonymous> 错误: Uncaught (in promise) TypeError: doc is not a function at HTMLButtonElement。<anonymous> - Error: Uncaught (in promise) TypeError: doc is not a function at HTMLButtonElement.<anonymous> 未捕获的类型错误:无法在 HTMLButtonElement 处设置 null 的属性“textContent”。<anonymous> (index.js:17),为什么会这样?</anonymous> - Uncaught TypeError: Cannot set property 'textContent' of null at HTMLButtonElement.<anonymous> (index.js:17), why is it happening? 错误出现为“Uncaught TypeError: notesObj.push is not a function at HTMLButtonElement。<anonymous> “。请任何人建议做什么</anonymous> - Error comes as "Uncaught TypeError: notesObj.push is not a function at HTMLButtonElement.<anonymous>" . Please anyone suggest what to do Javascript/Firestore: Uncaught (in promise) TypeError: firebase.firestore(...).collection(...).doc(...).collection(...).set 不是函数 - Javascript/Firestore: Uncaught (in promise) TypeError: firebase.firestore(...).collection(...).doc(...).collection(...).set is not a function React- Firebase: Uncaught TypeError: firebase.firestore is not a function - React- Firebase: Uncaught TypeError: firebase.firestore is not a function Firebase:TypeError:firebase.firestore 不是函数 - Firebase: TypeError: firebase.firestore is not a function 未捕获的类型错误:无法在 HTMLButtonElement 处读取 null(读取“推送”)的属性。<anonymous> (main.js:15:23)</anonymous> - Uncaught TypeError: Cannot read properties of null (reading 'push') at HTMLButtonElement.<anonymous> (main.js:15:23) TypeError: firebase.firestore() is not a function web javascript - TypeError: firebase.firestore () is not a function web javascript 未捕获的语法错误:HTMLButtonElement 中的无效或意外标记。<anonymous></anonymous> - Uncaught SyntaxError: Invalid or unexpected token at HTMLButtonElement.<anonymous>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM