未在snapshot.forEach中定义数据库以实现云功能

时间:2018-07-09 06:34:03

标签: firebase firebase-realtime-database

当前,我每分钟运行一次云功能以删除此类旧帖子

exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((change,context) => {
    const currentTime = Date.now()
    const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
    return getPostsForDate.get().then(snapshot => {
        const updates = {}
        const batch = admin.firestore().batch()
        snapshot.forEach((doc) => {
            var key = doc.id
            console.log(key)
            const convos =  database().ref('/convoID/' + key).once('value', (snapshot) => {
                 if (snapshot.exists){
                    for (var child in snapshot) {
                        const convoID = child
                        console.log(child+"shit")
                        updates["conversations/" + value] = null
                    }
                 }
             })
             updates["/convoID/"+ key] = null
             updates["/reveals/" + key] = null
             updates["/postDetails/" + key] = null
             const postFireStoreRef = admin.firestore().collection('posts').doc(key)
             const posterRef = admin.firestore().collection('posters').doc(key)
             batch.delete(postFireStoreRef)
             batch.delete(posterRef)
        })
        return admin.database().ref().update(updates), batch.commit()
})
})

从本质上讲,该函数查询firestore以获取时间戳小于当前时间的帖子。但是,当代码遍历每个帖子以查找相关对话时,似乎存在一个问题,特别是:

snapshot.forEach((doc) => {
        var key = doc.id
        console.log(key)
        const convos =  database().ref('/convoID/' + key).once('value', (snapshot) => {

在控制台中,我收到错误消息:database is not defined at snapshot.forEach最初,我的代码为admin.database().ref(),但仍然收到错误消息,未定义“值”。是否不可能为每个这样的帖子创建参考?语法不正确吗?

编辑后的代码以包含管理员:

exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((change,context) => {
    const currentTime = Date.now()
    const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
    return getPostsForDate.get().then(snapshot => {
        const updates = {}
        const batch = admin.firestore().batch()
        snapshot.forEach((doc) => {
            var key = doc.id
            console.log(key)
            const convos =  admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
                 if (snapshot.exists){
                    for (var child in snapshot) {
                        const convoID = child
                        console.log(child+"shit")
                        updates["conversations/" + value] = null
                    }
                 }
             })
             updates["/convoID/"+ key] = null
             updates["/reveals/" + key] = null
             updates["/postDetails/" + key] = null
             const postFireStoreRef = admin.firestore().collection('posts').doc(key)
             const posterRef = admin.firestore().collection('posters').doc(key)
             batch.delete(postFireStoreRef)
             batch.delete(posterRef)
        })
        return admin.database().ref().update(updates), batch.commit()
})
})

错误:

ReferenceError: value is not defined
at admin.database.ref.once (/user_code/index.js:262:52)
at onceCallback (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4843:51)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4465:22
at exceptionGuard (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:691:9)
at EventList.raise (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9727:17)
at EventQueue.raiseQueuedEventsMatchingPredicate_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9681:41)
at EventQueue.raiseEventsForChangedPath (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9665:14)
at Repo.update (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12891:30)
at Reference.update (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:13992:19)
at getPostsForDate.get.then.snapshot (/user_code/index.js:274:39)

1 个答案:

答案 0 :(得分:1)

要获取子密钥,请使用Snapshot.key。您还需要使用Snapshot.forEach来遍历/convoId/$key的子节点。

类似这样:

const convos =  admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
    snapshot.forEach((child) => {
        updates["conversations/" + child.key] = null
    }
 })