javascript - TypeError: subcollection.push 不是函数
问题描述
我正在使用 Cloud Firestore 并制作评论部分,以便用户可以发表评论。
我在下面找到的一些代码:
export function saveComment(comment, id, uid) {
// database = firebase.database().ref('posts/');
return dispatch => database.child(id).child('comments').push({
content: comment.content, uid })
}
上面的代码使用实时数据库,我不能使用 child() 作为 Cloud Firestore。
这是我的代码:
postActions.js
export const addComment = (postId, comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
const firebase = getFirebase();
const profile = getState().firebase.profile;
const user = firebase.auth().currentUser;
const ref = firestore.doc("posts/"+postId);
const subcollection = ref.collection('comments')
subcollection.push(comment)
};
};
而不是 child(),我需要使用什么样的函数?在 Cloud Firestore 中
解决方案
push()
您在使用 Cloud Firestore 时找到的方法来自 Firebase Realtime Database API。虽然这两个数据库都是 Firebase 的一部分,但它们是完全独立的,并且每个都有自己的 API。
要将新文档添加到 Cloud Firestore 集合,请使用add
其CollectionReference
. 所以:
subcollection.add(comment)
有关添加文档,请参阅Firestore 文档。
推荐阅读
- tableau-api - 客户订单数
- python - 使用提供的双变量函数将可变大小列表中的多个参数链连接起来
- c++ - 如何序列化 VARIANT 或 BSTR?
- linux - 即使命令不起作用,如何处理所有错误消息?
- np - NP 证明不平凡的问题
- c++ - 从调用这些线程的“母亲”类对象中的线程访问方法
- java - 为什么需要 @Test 注释?(Java JUnit)
- ios - MDC 因 'NSInvalidArgumentException' 而崩溃,原因:'-[UIButton applyOutlinedThemeWithScheme:]:无法识别的选择器已发送到实例
- java - Apache Kafka Streams:如何从使用内存键值存储切换到持久键值存储?
- audio - ffmpeg图像+音频转视频,文件很大?