Lau*_*fan 9 node.js firebase firebase-realtime-database google-cloud-functions firebase-storage
我是NodeJS的新手,我正在尝试为Firebase的Cloud Functions编写下一个方法.
我想要实现的目标:
这些是我的Firebase数据库结构:
照片/ {userUID}/{} photoUID
{
"dateCreated": "2017-07-27T16:40:31.000000Z",
"isProfilePhoto": true,
"isSafe": true,
"uid": "{photoUID}",
"userUID": "{userUID}"
}
Run Code Online (Sandbox Code Playgroud)
和Firebase存储格式:
照片/ {userUID}/{} photoUID .PNG
我正在使用的NodeJS代码:
const functions = require('firebase-functions')
const googleCloudStorage = require('@google-cloud/storage')({keyFilename: 'firebase_admin_sdk.json' })
const admin = require('firebase-admin')
const vision = require('@google-cloud/vision')();
admin.initializeApp(functions.config().firebase)
exports.sanitizePhoto = functions.database.ref('photos/{userUID}/{photoUID}')
.onDelete(event => {
let photoUID = event.data.key
let userUID = event.data.ref.parent.key
console.log(`userUID: ${userUID}, photoUID: ${photoUID}`);
if (typeof photoUID === 'undefined' || typeof userUID === 'undefined') {
console.error('Error while sanitize photo, user uid or photo uid are missing');
return
}
console.log(`Deleting photo: ${photoUID}`)
googleCloudStorage.bucket(`photos/${userUID}/${photoUID}.png`).delete().then(() => {
console.log(`Successfully deleted photo with UID: ${photoUID}, userUID : ${userUID}`)
}).catch(err => {
console.log(`Failed to remove photo, error: ${err}`)
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到下一个错误:"ApiError:Not found"
我认为代码的这些部分是导致问题的部分:
googleCloudStorage.bucket(`photos/${userUID}/${photoUID}.png`).delete()
Run Code Online (Sandbox Code Playgroud)
提前感谢您的支持和耐心.
Lau*_*fan 10
发现问题,这里的代码对我有用:
const functions = require('firebase-functions');
Run Code Online (Sandbox Code Playgroud)
实时数据库:
exports.sanitizePhoto = functions.database.ref('photos/{userUID}/{photoUID}').onDelete(event => {
let photoUID = event.data.key
let userUID = event.data.ref.parent.key
console.log(`userUID: ${userUID}, photoUID: ${photoUID}`);
if (typeof photoUID === 'undefined' || typeof userUID === 'undefined') {
console.error('Error while sanitize photo, user uid or photo uid are missing');
return
}
console.log(`Deleting photo: ${photoUID}`)
const filePath = `photos/${userUID}/${photoUID}.png`
const bucket = googleCloudStorage.bucket('myBucket-12345.appspot.com')
const file = bucket.file(filePath)
file.delete().then(() => {
console.log(`Successfully deleted photo with UID: ${photoUID}, userUID : ${userUID}`)
}).catch(err => {
console.log(`Failed to remove photo, error: ${err}`)
});
});
Run Code Online (Sandbox Code Playgroud)
这里是相同的代码,但对于Firestore(不确定它是否有效,因为我不是NodeJS开发人员,并且实际上没有测试它):
exports.sanitizePhoto = functions.firestore.document('users/{userUID}/photos/{photoUID}').onDelete((snap, context) =>{
const deletedValue = snap.data();
let photoUID = context.params.photoUID
let userUID = context.params.userUID
console.log(`userUID: ${userUID}, photoUID: ${photoUID}`);
if (typeof photoUID === 'undefined' || typeof userUID === 'undefined') {
console.error('Error while sanitize photo, user uid or photo uid are missing');
return
}
console.log(`Deleting photo: ${photoUID}`)
const filePath = `photos/${userUID}/${photoUID}.png`
const bucket = googleCloudStorage.bucket('myBucket-12345.appspot.com')
const file = bucket.file(filePath)
file.delete().then(() => {
console.log(`Successfully deleted photo with UID: ${photoUID}, userUID : ${userUID}`)
}).catch(err => {
console.error(`Failed to remove photo, error: ${err}`)
});
});
Run Code Online (Sandbox Code Playgroud)
你也可以注意到我的路径改变了:
photos/{userUID}/{photoUID}
Run Code Online (Sandbox Code Playgroud)
至:
users/{userUID}/photos/{photoUID}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4325 次 |
| 最近记录: |