Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud function: add value to firebase database

Whenever any file added to firebase storage, it's path value should be stored in firebase database by firebase cloud function

I have achieved by following code

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    
    });
});

Entry is showing in database like below

enter image description here

I want it like below

enter image description here

I have tried a lot but could not be able to achieve it, will any body help me where should I need to change so I can achieve code like I want

like image 401
Siddhpura Amit Avatar asked Sep 08 '17 14:09

Siddhpura Amit


2 Answers

Thanks to @DoesData finally I have achieved by following way

 admin.database().ref().child(newPostKey).set(filePath);
like image 185
Siddhpura Amit Avatar answered Nov 11 '22 04:11

Siddhpura Amit


Strange, but maybe this will work:

const filePath = object.name;
const newPostKey = admin.database().ref().push().key;   
admin.database().ref().update({
    [newPostKey] : filepath  
});
like image 36
J. Doe Avatar answered Nov 11 '22 05:11

J. Doe