Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 对象在TypeScript中可能未定义_Javascript_Typescript_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 对象在TypeScript中可能未定义

Javascript 对象在TypeScript中可能未定义,javascript,typescript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Typescript,Firebase,Google Cloud Firestore,Google Cloud Functions,我确实使用了sfDoc!==未定义,但我仍然得到对象的错误可能未定义。我做错什么了吗 return database.runTransaction(function (transaction) { return transaction.get(sfDocRef).then(sfDoc => { if (!sfDoc.exists) { throw "Document does not exist!"; } if

我确实使用了
sfDoc!==未定义
,但我仍然得到对象的错误可能未定义。我做错什么了吗

return database.runTransaction(function (transaction) {
    return transaction.get(sfDocRef).then(sfDoc => {
        if (!sfDoc.exists) {
            throw "Document does not exist!";
        }
        if (sfDoc !== undefined) {
            var usedCount = sfDoc.data().usedCount + 1;
            transaction.update(sfDocRef, { usedCount: usedCount });    
        }
        return transaction;
    });
}).then(function () {
    console.log("Tag field changed!");
    return true;
}).catch(function (error) {
    console.log("Error in changing Tag field: ", error);
    return false;
});

试试这个例子。检查sfDoc并返回
事务。更新
,然后等待解决承诺。根据文档,您不必检查
sfDoc
。它将始终被定义

return database
  .runTransaction(function (transaction) {
    return transaction.get(sfDocRef).then((sfDoc) => {
      if (sfDoc && sfDoc.exists) {
        var usedCount = sfDoc.data().usedCount + 1;
        return transaction.update(sfDocRef, { usedCount: usedCount });
      } else {
        throw "Document does not exist!";
      }
    });
  })
  .then(function () {
    console.log("Tag field changed!");
    return true;
  })
  .catch(function (error) {
    console.log("Error in changing Tag field: ", error);
    return false;
  });

您第一次使用的
!sfDoc.exists
,因此,如果未定义
sfDoc
,则会出现错误believe@CodeManiac如果我删除
,仍然会出现错误!sfDoc.exists
这是在Firebase还是香草云函数中发生的?