【问题标题】:Variable userFetched is used before being assigned in Firebase cloud functions在 Firebase 云函数中分配之前使用变量 userFetched
【发布时间】:2020-04-13 08:55:59
【问题描述】:

有很多类似的问题,但没有一个能解决我的问题。

这是我的代码:

if (commandFn === "/fetch") {
  let userFetched: string;
  returnText = `${commandFn} used with ${commandArg}`;
  admin.database
    .ref(`/Users/` + commandArg + "/userdata")
    .once("value")
    .then((snapshot) => {
      userFetched = snapshot.val().userdata;
    });

  return res.status(200).send({
    method: "sendMessage",
    chat_id,
    text: `${returnText} /n ${userFetched}`,
  });
}

为什么说变量没有赋值?在我不必使用 admin.database 功能的其他函数中,我只是在 if 块中添加了 return 语句。

我的代码中有很多 if-else 语句会更改要返回的文本,但会出现相同的错误。所以我必须在每个 if-else 块中一直添加 return 语句。

但是当我使用 admin.database 功能时,我不能这样做。 我再次看到其他类似的问题,但没有回答我的问题。

这是面临同样问题的另一段代码:

let numberOfWords: number
    for (let index = 1 ; index <= userCommandSlicedLength; index++) {  
      let lastChar: string = '' 
      let currentChar: string = ''
      currentChar = userCommandSliced.charAt(index)
      lastChar = userCommandSliced.charAt(index-1)
      if (currentChar === " " && lastChar !== " ") {
        numberOfWords = numberOfWords + 1
      }  
      else if (currentChar === " " && lastChar === " ") {    // This is a test String.
       numberOfWords = numberOfWords + 0 
      }   
    }

    const finalNumberOfWords: number = numberOfWords

    console.log(`Number of words final are = ${finalNumberOfWords}`)

第二个代码的问题:

src/index.ts:88:25 - error TS2454: Variable 'numberOfWords' is used before being assigned.

88         numberOfWords = numberOfWords + 1
                           ~~~~~~~~~~~~~

src/index.ts:91:24 - error TS2454: Variable 'numberOfWords' is used before being assigned.

91        numberOfWords = numberOfWords + 0
                          ~~~~~~~~~~~~~

src/index.ts:95:40 - error TS2454: Variable 'numberOfWords' is used before being assigned.

95     const finalNumberOfWords: number = numberOfWords

【问题讨论】:

    标签: typescript firebase google-cloud-functions


    【解决方案1】:

    必须用值初始化然后才可以添加值

    userFetched
    

    被声明为字符串,我们不能在那里添加对象,将其初始化为对象。

    numberOfWords
    

    应该用 0 初始化,这样你就可以递增它

    【讨论】:

      【解决方案2】:

      变量userFetched 未定义,因为它是在Promise.then() 中异步分配的。

      以下代码应按预期工作。

      if (commandFn === "/fetch") {
        let userFetched: string;
        returnText = `${commandFn} used with ${commandArg}`;
        admin.database
          .ref(`/Users/` + commandArg + "/userdata")
          .once("value")
          .then((snapshot) => {
            userFetched = snapshot.val().userdata;
            return res.status(200).send({
              method: "sendMessage",
              chat_id,
              text: `${returnText} /n ${userFetched}`,
            });
          })
          .catch((error) => console.error(error));
      }
      

      基于 cmets 编辑

      这就是我要减少 res.send 语句的方法。

      async function MyCloudFunction() {
        let userFetched: string;
        if (commandFn === "/fetch") {
          const snapshot = await admin.database
            .ref(`/Users/` + commandArg + "/userdata")
            .once("value");
          userFetched = snapshot.val().userdata;
        }
        //   DO OTHER STUFF
        return res.status(200).send({
          method: "sendMessage",
          chat_id,
          text: `${returnText} /n ${userFetched}`,
        });
      }
      

      【讨论】:

      • 它返回值未定义。 userFetched 的值未定义.. 路径正确
      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 2019-11-06
      • 2021-08-16
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2019-01-19
      相关资源
      最近更新 更多