'Firebase Function - finished with status code: 204
I have a firebase cloud function:
const admin = require('firebase-admin');
const firebase_tools = require('firebase-tools');
const functions = require('firebase-functions');
admin.initializeApp();
exports.deleteUser = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall((data, context) => {
const userId = context.auth.uid;
var promises = [];
// DELETE DATA
var paths = ['users/' + userId, 'messages/' + userId, 'chat/' + userId, 'like/' + userId];
paths.forEach((path) => {
promises.push(
recursiveDelete(path).then( () => {
return 'success';
}
).catch( (error) => {
console.log('Error deleting user data: ', error);
})
);
});
// DELETE FILES
const bucket = admin.storage().bucket();
var image_paths = ["avatar/" + userId, "avatar2/" + userId, "avatar3/" + userId];
image_paths.forEach((path) => {
promises.push(
bucket.file(path).delete().then( () => {
return 'success';
}
).catch( (error) => {
console.log('Error deleting user data: ', error);
})
);
});
// DELETE USER
promises.push(
admin.auth().deleteUser(userId)
.then( () => {
console.log('Successfully deleted user');
return true;
})
.catch((error) => {
console.log('Error deleting user:', error);
})
);
return Promise.all(promises).then(() => {
return true;
}).catch(er => {
console.error('...', er);
});
});
function recursiveDelete(path, context) {
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
.then(() => {
return {
path: path
}
}).catch( (error) => {
console.log('error: ', error);
return error;
});
}
// [END recursive_delete_function]
Im trying to run this function locally from a javascript file:
function deleteAccount(userId) {
fetch(
`https://us-central1-myappname.cloudfunctions.net/deleteUser/${userId}`,
{ method: "DELETE" }
);
}
But im getting a:
deleteUser Function execution took 1952 ms, finished with status code: 204
Here is my cloud function URL from firebase:
Solution 1:[1]
Posting an answer based on my comment and above community members comments for visibility.
Based on the comment from stf_F it is a good idea to use callable functions as it remembers the state, and you don't have to pass any UID to delete the user. Or you can delcare it also as mentioned in this document.
const text = data.text; // Authentication / user information is automatically added to the request. const uid = context.auth.uid; const name = context.auth.token.name || null; const picture = context.auth.token.picture || null; const email = context.auth.token.email || null;index.js
And you can check how to delete using callable from the following document.
However, if you prefer using fetch and if you want to know how to get the data in JSON and how to save it in HTML. You can check this article. You can use the following example regarding how to structure your fetch
fetch(functionEndpoint) .then(res => res.json()) .then(function(data) { /** DO STUFF WITH YOUR DATA **/ }).catch(function(error) { console.log("Err is:" + err); });
And Below example how to use get and fetch the info you need.
document.getElementById("output").innerHTML = JSON.stringify(data, null, 2); }
Please don't forget to specify the output in your HTML page
<pre id="output"> </pre>
And you can check the following document of how to parse the received JSON data and extract UID for example.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rajeev Tirumalasetty |

