在下面的示例中,我想在docPath中创建一个新集合。是否可以在docPath(doc.ref.path)之后继续使用.collection()。doc()...?我不断收到此错误:无法读取未定义的属性“集合”。
出现错误的我的函数:
function comment(docPath) {
var modalcomment = document.getElementById("modal-comment");
modalcomment.style.display = "block";
var submitcomment = document.getElementById("submitcomment");
submitcomment.addEventListener('click', (e) => {
var commentinput = document.getElementById("comment");
db.docPath.collection('comments').doc().set({
comment: commentinput.value,
})
})
}
我存储docPath变量的代码:
firebase.auth().onAuthStateChanged(function(user) {
var query = db.collectionGroup("userPosts")
query.get().then(querySnapshot => {
setupPosts(querySnapshot.docs)
})
const posts = document.querySelector('.posts');
const setupPosts = (data) => {
let html = '';
data.forEach(doc => {
var docPath = doc.ref.path;
console.log(docPath)
const post = doc.data();
const picURL = post.picURL;
let li = `<li class="post">
<div class="title">${post.title}</div>
<div class="content">${post.content}</div>
<button class="comment" onclick="comment('${docPath}')">Comment</button>
<button class="like" onclick="like('${docPath}')">Like</button>`;
li += (post.picURL ? `<img class="img" src="${post.picURL}" onclick="openImage('${picURL}')">` : ``);
li += `</li><br></br>`;
html += li
})
posts.innerHTML = html;
}
docPath显示正确的存储数据(文档的正确路径)
答案 0 :(得分:1)
否,doc.ref.path
是字符串,无法使用。您不能在字符串对象上调用collection()
。
也许您想改为存储doc.ref
对象DocumentReference的值。那肯定有一个collection()方法,您可以使用该方法为该文档下的子集合建立更深入的引用。