Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何处理firebase云函数api oncall的错误?_Javascript_Firebase_Firebase Authentication_Google Cloud Functions_Firebase Admin - Fatal编程技术网

Javascript 如何处理firebase云函数api oncall的错误?

Javascript 如何处理firebase云函数api oncall的错误?,javascript,firebase,firebase-authentication,google-cloud-functions,firebase-admin,Javascript,Firebase,Firebase Authentication,Google Cloud Functions,Firebase Admin,如何处理firebase admin api oncall的错误?我应该抛出错误还是返回响应?我是否正确地执行了成功返回响应 exports.registerVendor = functions.https.onCall(async (data, context) => { const email = data.email; const displayName = data.firstName + data.lastName; // Checking attri

如何处理firebase admin api oncall的错误?我应该抛出错误还是返回响应?我是否正确地执行了成功返回响应

exports.registerVendor = functions.https.onCall(async (data, context) => {

    const email = data.email;
    const displayName = data.firstName + data.lastName;

    // Checking attribute.
    if (!(typeof email === 'string') || email.length === 0 || 
        !(typeof displayName === 'string') || displayName.length === 0) {
        // Throwing an HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
            'one arguments "text" containing the message text to add.');
    }

    // const uid = context.auth?.uid;

    // Checking that the user is authenticated.
    if (!context.auth) {
        // Throwing an HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError('failed-precondition', 
            'The function must be called ' + 'while authenticated.');
    }

    try {
        const createResponse = await admin.auth().createUser({
            email: email,
            emailVerified: false,
            password: '123123',
            displayName: displayName,
            disabled: false
          })

        console.log(createResponse);

        return {
            data: {
                uid: createResponse.uid
            },
            status: 200,
            code: "Success"
        };
    } catch (err) {
        console.log(err as Error);
        // throw new functions.https.HttpsError(err.code, err.message);
        return {
            error: err,
        }
    }
});
上的文档包含以下示例:

因此,这是从服务器向调用者获取错误条件的推荐方法,然后您将。但您也可以像现在这样返回自己的错误代码。

上的文档包含以下示例:


因此,这是从服务器向调用者获取错误条件的推荐方法,然后您将。但您也可以像现在这样返回自己的错误代码。

如果此代码在可调用的云函数中,请编辑您的问题以包括整个函数,以及您可能如何调用它。注意,已更改。如果此代码在可调用的云函数中,请编辑您的问题以包括整个函数,以及你如何称呼它。注意到,改变了。如前所述,文档推荐了另一种方式。但是,如果你所拥有的对你有用的话,我觉得很好。如前所述,文档推荐了另一种方法。但如果你所拥有的对你有用的话,我觉得很好。
// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
      'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
      'while authenticated.');
}