【问题标题】:Firebase Cloud function: add value to firebase databaseFirebase Cloud 功能:为 Firebase 数据库增值
【发布时间】:2018-02-17 13:13:10
【问题描述】:

当任何文件添加到firebase存储时,它的路径值应该通过firebase云功能存储在firebase数据库中

我通过以下代码实现了

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.generateThumbnail = functions.storage.object().onChange(event => {
    const object = event.data;
    const resourceState = object.resourceState;
    // Exit if this is a move or deletion event.
    if (resourceState === 'not_exists') {
      console.log('This is a deletion event.');
      return;
    }
    const filePath = object.name; // File path in the bucket.
    console.log(filePath);

    const newPostKey = admin.database().ref().push().key;   

    admin.database().ref().child(newPostKey).set({
        filePath    
    });
});

条目显示在数据库中,如下所示

我想要像下面这样

我已经尝试了很多但无法实现它,任何人都可以帮助我在哪里需要更改以便我可以实现我想要的代码

【问题讨论】:

  • 记录filePath时打印什么?只是网址?试试 admin.database().ref().child(newPostKey).set(filePath);
  • 非常感谢@DoesData

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


【解决方案1】:

感谢@DoesData,我终于通过以下方式实现了

 admin.database().ref().child(newPostKey).set(filePath);

【讨论】:

    【解决方案2】:

    奇怪,但也许这会起作用:

    const filePath = object.name;
    const newPostKey = admin.database().ref().push().key;   
    admin.database().ref().update({
        [newPostKey] : filepath  
    });
    

    【讨论】:

    • 嗨@J.Doe 我已经尝试过你的方法,但在 Firebase 日志 ReferenceError 中出现以下错误:未定义文件路径
    • 也感谢您的帮助
    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多