简体   繁体   English

Firebase Function - 完成状态码:204

[英]Firebase Function - finished with status code: 204

I have a firebase cloud function:我有一个 firebase 云 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:我试图从 javascript 文件在本地运行这个 function:

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 deleteUser Function 执行耗时 1952 毫秒,完成状态码:204

Here is my cloud function URL from firebase:这是我的云 function URL 来自 firebase:

在此处输入图像描述

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.根据 stf_F 的评论,使用可调用函数是个好主意,因为它会记住 state,而且您不必传递任何 UID 即可删除用户。 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 .您可以从以下文档中检查如何使用 callable 删除。

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.不过,如果你更喜欢使用fetch,想知道如何获取JSON中的数据,以及如何保存HTML中的数据,可以查看这篇文章。 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请不要忘记在您的 HTML 页面中指定 output

<pre id="output"> </pre>

And you can check the following document of how to parse the received JSON data and extract UID for example.您可以查看以下文档,了解如何解析收到的 JSON 数据并提取 UID,例如。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云 Function 完成状态:“响应错误” - Firebase Cloud Function finished with status: 'response error' Firebase Function 执行耗时 2794 毫秒,完成状态为:“确定”,...但 Firestore 中没有发生任何变化 - Firebase Function execution took 2794 ms, finished with status: 'ok' ,... but no change took place in Firestore 云 Function 完成状态:向 Firestore 添加数据时“超时” - Cloud Function finished with status: 'timeout' while adding data to Firestore Function 执行耗时 540029 毫秒,完成状态:“超时” - Function execution took 540029 ms, finished with status: 'timeout' Firebase 中的异步 function 甚至在 function 主体完成执行之前就解析了 promise - Async function in Firebase resolves the promise even before the function body has finished executing Typesense 扩展完成状态:超时 - Typesense extension finished with status: timeout 为什么我的 CoroutineScope 在它有 Firebase 数据并完成之前不停止代码? - Why doesnt my CoroutineScope stop the code until it has the Firebase data and is finished? API 抛出错误的响应代码 500 内部错误而不是 204 - The API is throwing wrong response code 500 internal error instead 204 等待云Function完成 - Wait for Cloud Function to be finished Firebase function 部署失败,需要外部代码 - Firebase function deploy fails with requiring external code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM