"Cannot read property 'uid' of undefined" 错误 - 格子 link 令牌

"Cannot read property 'uid' of undefined" error - Plaid link token

我在尝试创建格子标记时收到“无法读取未定义的 属性 'uid'”....我花了大约 3 天的时间来让它工作。

有人知道怎么解决吗?

获取格子代币的云函数

//PLAID - create link Token plaid Nat
const plaid = require("plaid");

exports.createPlaidLinkToken = functions.https.onCall(async (data, context) => {
  const customerId = context.auth.uid;

  const plaidClient = new plaid.Client({
    clientID: functions.config().plaid.client_id,
    secret: functions.config().plaid.secret,
    env: plaid.environments.development,
    options: {
      version: "2019-05-29",
    },
  });

  return plaidClient.createLinkToken({
      user: {
        client_user_id: customerId,
      },
      client_name: "Reny",
      products: ["auth"],
      country_codes: ["US"],
      language: "en",
    })
    .then((apiResponse) => {
      const linkToken = apiResponse.link_token;
      return linkToken;
    })
    .catch((err) => {
      console.log(err);
      throw new functions.https.HttpsError(
        "internal",
        " Unable to create plaid link token: " + err
      );
    });
});

swift 函数

    class func createLinkToken(completion: @escaping (String?) -> ()){
        //this is the firebase function
        Functions.functions().httpsCallable("createPlaidLinkToken").call { (result, error) in
            if let error = error {
                debugPrint(error.localizedDescription)
                return completion(nil)
            }
            guard let linkToken = result?.data as? String else {
                return completion(nil)
            }
            completion(linkToken)
        }
    }

您的代码中唯一的 .uid 是在这一行中:

const customerId = context.auth.uid;

所以 context.auth 好像是 undefined。在 Cloud Functions 代码中,您可以使用以下方法处理此问题:

exports.createPlaidLinkToken = functions.https.onCall(async (data, context) => {
  // 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.');
  }

  const customerId = context.auth.uid;
  ...

此处的新代码来自 handling errors in callable Cloud Functions 上的 Firebase 文档。

您还需要在 Swift 代码中 check if the user is signed in