当您通过Firebase云功能删除文件(特别是在本例中为照片)时,某些文件会被删除,有些文件会保留在Firebase存储中。当文件未被删除时,我在日志中收到错误,即错误文本。
Error: read ECONNRESET at exports._errnoException (util.js:1026:11) at TCP.onread (net.js:569:26)
Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TCP.onread (net.js:569:26)
我的代码
var functions = require('firebase-functions');
const admin = require('firebase-admin');
const gcs = require('@google-cloud/storage')();
admin.initializeApp(functions.config().firebase);
// deleting functions
exports.userDidDeleted = functions.auth.user().onDelete(event => {
const user = event.data; // The Firebase user.
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
const userSearchLocationModelPath = '/userSearchLocationModel/' + user.uid;
console.log('userDidDeleted', userSearchLocationModelPath);
admin.database().ref(userSearchLocationModelPath).remove();
// cards
var cardsRef = admin.database().ref('cards')
cardsRef.orderByChild('ownerID').equalTo(user.uid).on("child_added", function(snapshot) {
console.log('cardsRef', snapshot.key, snapshot.val);
const cardRef = '/cards/' + snapshot.key;
admin.database().ref(cardRef).remove();
// location
admin.database().ref('cardLocation').child(snapshot.key).remove();
// photo
const filePath = 'cardImages/' + snapshot.key + '/mainCardPhoto/' + 'main.jpg';
const bucket = gcs.bucket('example-example7.appspot.com');
const file = bucket.file(filePath);
const pr = file.delete();
});
});
答案 0 :(得分:0)
在咨询Jen Person之后,我改变了代码,它对我有用。
//删除功能
exports.userDidDeleted = functions.auth.user().onDelete(event => {
const user = event.data; // The Firebase user.
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
const userSearchLocationModelPath = '/userSearchLocationModel/' + user.uid;
console.log('userDidDeleted', userSearchLocationModelPath);
admin.database().ref(userSearchLocationModelPath).remove();
// cards
var cardsRef = admin.database().ref('cards')
cardsRef.orderByChild('ownerID').equalTo(user.uid).on("child_added", function(snapshot) {
// there, I queried!
console.log('cardsRef', snapshot.key, snapshot.val);
const cardRef = '/cards/' + snapshot.key;
admin.database().ref(cardRef).remove();
// location
admin.database().ref('cardLocation').child(snapshot.key).remove();
// photo
const firebasestoragePath = 'firebasestorage.googleapis.com';
const placeVenuePhotoPath = '' + snapshot.child('photoURLsProperties').child('placeVenuePhoto').val();
if (placeVenuePhotoPath.includes(firebasestoragePath)) {
const filePath = 'cardImages/' + snapshot.key + '/mainCardPhoto/' + 'main.jpg';
const bucket = gcs.bucket('example-example7.appspot.com');
const file = bucket.file(filePath);
const pr = file.delete();
return console.log(filePath, "is deleted");
} else {
return console.log("card did not have a custom image", snapshot.key);
}
});