我想获取image
的值。
我已经具有用户的UID,但是当我的代码运行时,出现此错误:Error: No such object: 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);
});
预先感谢您的帮助。
答案 0 :(得分:1)
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());
现在,这将记录图像的路径,因为它存储在数据库中。