Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获得孩子的价值_Node.js_Firebase_Firebase Realtime Database_Google Cloud Functions_Google Cloud Storage - Fatal编程技术网

Node.js 获得孩子的价值

Node.js 获得孩子的价值,node.js,firebase,firebase-realtime-database,google-cloud-functions,google-cloud-storage,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,Google Cloud Storage,我想得到图像的价值 我已经有了用户的UID,但是当我的代码运行时,我得到了以下错误:错误:没有这样的对象:XXXXX.appspot.com/https://XXXXX.firebaseio.com/images/-M9OeGPXB-KqEVqxZYCb/image 我不明白为什么,因为这个值不是空的 我的数据库截图: 我的代码: // The Cloud Functions for Firebase SDK to create Cloud Functions and setup trigg

我想得到图像的价值

我已经有了用户的UID,但是当我的代码运行时,我得到了以下错误:错误:没有这样的对象:XXXXX.appspot.com/https://XXXXX.firebaseio.com/images/-M9OeGPXB-KqEVqxZYCb/image

我不明白为什么,因为这个值不是空的

我的数据库截图:

我的代码:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
var admin = require("firebase-admin");

var serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://XXXXXX.firebaseio.com",
  storageBucket: "gs://XXXXX.appspot.com",
});

var defaultStorage = admin.storage();

// Cut off time. Child nodes older than this will be deleted.
const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 24 Hours in milliseconds.

/**
 * This database triggered function will check for child nodes that are older than the
 * cut-off time. Each child needs to have a `timestamp` attribute.
 */
exports.deleteOldItems = functions.database.ref('/images/{pushId}').onWrite(async (change) => {
  const ref = change.after.ref.parent; // reference to the parent
  const now = Date.now();
  const cutoff = now - CUT_OFF_TIME;
  const oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  const snapshot = await oldItemsQuery.once('value');


  snapshot.forEach((child) => {

        const uid = child.key;
        const re = admin.database().ref('images/'+uid);
        const img = re.child('image');

        const bucket = defaultStorage.bucket();
        const file = bucket.file(img);

    // Delete the file
        return file.delete();
    })


  // create a map with all children that need to be removed
  const updates = {};
  snapshot.forEach(child => {
    updates[child.key] = null;
  });
  // execute all updates in one go and return the result to end the function
  return ref.update(updates);
});
提前感谢您的帮助。

img变量只是对数据库中属性的引用。如果在上调用toString,则返回该引用的完整路径,即https://yourprojct.firebaseio.com/path/to/property.

因此,您需要实际使用数据库中的值,并在中使用该值查找存储中的文件:

snapshot.forEach((child) => {
    const uid = child.key;
    const img = child.child('image');
    console.log(img.val());
现在,这将记录图像的路径,因为它存储在数据库中