云功能:检查文档是否存在总是返回存在

Kar*_*dts 1 firebase google-cloud-functions google-cloud-firestore

我正在使用此云函数(Typescript)检查文档是否存在。问题:文档没有退出,返回存在...

非常感谢您的帮助和努力!

export const repeat2 = functions.https.onCall((data, context) => {

  console.log(data.message);
  console.log(data.count);

  const getDocument = admin.firestore().collection('key').doc(data.message).get();

        if(getDocument != null) {
          console.log('EXISTS');
        }

        else {console.log("doens't exist");}

    return {
        repeat_message: data.message,
    }
  });
Run Code Online (Sandbox Code Playgroud)

Ale*_*ksi 5

get()返回一个Promise,而不是实际的文档。另外,您需要使用.exists已解析的值;在这里检查文档

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});
Run Code Online (Sandbox Code Playgroud)