青春有我
2022-12-08 15:52:48
我正在嘗試將我在表單中輸入的數據設置為 Firebase 中的字段名稱。$("#blogUID").val()它在第 3 行(博客/blogUID 集合)中工作得很好。但是,當我嘗試在 Stats/Blogs 集合(第 10 行)中設置字段名稱時,出現以下錯誤:error.ts:166 Uncaught FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined at new ui (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:52471) at Vh (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:164122) at kh (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:163328) at _d.doc (https://www.gstatic.com/firebasejs/7.17.1/firebase-firestore.js:1:280502) at HTMLButtonElement.<anonymous> (http://localhost:8080/TeamSolheim_Page1.html:251:36) at HTMLButtonElement.dispatch (https://code.jquery.com/jquery-3.5.1.min.js:2:43090) at HTMLButtonElement.v.handle (https://code.jquery.com/jquery-3.5.1.min.js:2:41074)1 $("#blog-submit-button").click(function(){2 // Add a new document in collection3 // db.collection("Blogs").doc($("#blogUID").val()).set({4 // blogDate: $("#blogDate").val(),5 // blogLink: $("#blogLink").val(),6 // blogTitle: $("#blogTitle").val(),7 // })8 // .then(function() { 9 db.collection("Stats").doc($("Blogs").val()).set({10 $("#blogUID").val(): $("#blogLikes").val(),11 })12 .then(function() { console.log("Document successfully written!"); $("#blog-form").hide(); $("#create-form").show(); }) .catch(function(error) { console.error("Error writing document 2: ", error); }); // }) // .catch(function(error) { // console.error("Error writing document 1: ", error); // }); })權限對任何經過身份驗證的用戶都是開放的: match /Stats/Blogs { allow read; allow write: if request.auth != null; }
2 回答

小怪獸愛吃肉
TA貢獻1852條經驗 獲得超1個贊
向下移動到格式化的答案:
.set() 需要一個對象——你在第一次嘗試時就把它“靠近”了。嘗試這個:
const blogger = $("#blogUID").val() // jQuery
const bloggerLikes = $("#blogLikes").val() // jQuery
db.collection("Stats").doc("Blogs").set( { [ blogger]: bloggerLikes } )
你需要[ ] 來使用 blogger 值作為索引/字段名

函數式編程
TA貢獻1807條經驗 獲得超9個贊
從堆棧跟蹤來看,您的錯誤似乎與您嘗試在第 9 行查詢文檔的方式有關。從第 3 行開始,正確db.collection("Blogs").doc($("#blogUID").val()).set()
搜索形式blogUID
,其中基于的查詢id
似乎是有效的。
但是,在您的第 9 行中,這db.collection("Stats").doc($("Blogs").val()).set()
不會返回基于 an 的查詢,因為您可以注意到函數中和id
之間的區別。由于錯誤通知問題在未定義范圍內,請更改您在第 9 行中調用的方式,以遵循第 3 行中的相同模式。#blogUID
Blogs
.doc()
.doc()
添加回答
舉報
0/150
提交
取消