【问题标题】:How to access deep value of realtime db with cloud function?如何通过云功能访问实时数据库的深层价值?
【发布时间】:2019-09-19 21:48:20
【问题描述】:

childSnapshot.val().k我有这个带云功能的:

{ '-LdmZIlKZh3O9cR8MOBU': 
   { id: 'ceccomcpmoepincin3ipwc',
     k: 'test',
     p: 'somepath',
     t: 1556700282278,
     u: 'username' },
 '-Llkocp3ojmrpemcpo3mc': 
   { id: '[epc[3pc[3m,',
     k: 'test2',
     p: 'somepath2',
     t: 1556700292290,
     u: 'username2' }
 }

我需要每个路径值,以便从存储中删除该文件。如何访问这个值?

我用于刷新状态、从存储中删除和删除文件的云功能:

var db = admin.database();

var ref = db.ref('someref');

ref.once("value").then((snapshot) => {
var updates = {};

var patObject = {
    fid: null,
    ft: null,
    ftr: null,
    fu: null,
    id: null,
    lid: null,
    lt: null,
    ltr: null,
    lu: null,
    t: null,
    tr: null,
    v: null,
    g: null,
    l: null,
    k: null
    };

    snapshot.forEach((childSnapshot) => {
        if(childSnapshot.numChildren() >= 14){
                var t = childSnapshot.val().t;

            if((t===1 || t===5) && childSnapshot.val().tr > 0) {
                if(childSnapshot.val().tr - 12 > 0){
                    updates[childSnapshot.key + '/tr'] = childSnapshot.val().tr - 12;

            if(childSnapshot.val().k !== ""){

              console.log('path: ', childSnapshot.val().k);

              childSnapshot.val().k.snapshot.forEach(kpath => {
                console.log('path: ', "path");
              });
            }
                } else {
                    updates[childSnapshot.key] = patObject;
                }
            }

            if(childSnapshot.val().tr<=0){
                updates[childSnapshot.key] = patObject;
            }
        } else {
            updates[childSnapshot.key] = patObject;
        }
  });

    ref.update(updates);

    res.send("");   

    return "";
}).catch(reason => {
    res.send(reason);
})

return "";  

【问题讨论】:

  • 你能分享你的云函数的整个代码吗?
  • 是的,没问题...
  • “从存储中删除该文件”是什么意思?来自实时数据库或 Cloud Storage for Firebase
  • 您的云函数代码中没有childSnapshot.val().kom
  • 我在实时数据库中有用于 Firebase 存储的路径,我想获取该路径以便在从实时数据库中删除此对象时删除图片...

标签: firebase firebase-realtime-database google-cloud-functions google-cloud-storage


【解决方案1】:

如果要删除ps的值对应的所有文件,需要使用Promise.all()并行执行异步删除任务(因为delete()方法返回一个Promise)。您需要遍历包含 p 路径的对象。

你的代码不容易看懂,所以你会在下面找到与上述解释相对应的部分。您可以将其集成到您的代码中!

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const defaultStorage = admin.storage();  //Note this line

//.....

exports.date = functions.https.onRequest((req, res) => { //I understand that you use an HTTP Cloud Function

   //....

   .then(...

       // Somehow you get the object you mention in your question, through childSnapshot.val().k

        const kObject = childSnapshot.val().k;

        const bucket = defaultStorage.bucket(yourFileBucket);

        const promises = [];

        Object.keys(kObject).forEach(
          //The values of the path p are obtained via kObject[key].p
          //Based on that we push the Promise returned by delete() to the promises array
          promises.push(bucket.file(kObject[key].p).delete());        
        );

        return Promise.all(promises)   
    .then(results => {
        //Here all the Promises that were in the promises array are resolved, which means that all the files are deleted
        res.send({result: results.length + ' files(s) deleted'});
    })
    .catch(error => {
        res.status(500).send(error);
    });


});

观看 Doug Stevenson 的以下官方 Firebase 视频可能会感兴趣:https://youtu.be/7IkUgCLr5oA

【讨论】:

    猜你喜欢
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多