简体   繁体   English

如何从 firebase 函数返回 promise 和 object?

[英]How do i return a promise and object from firebase functions?

Just a heads up, this my first day using cloud functions.提醒一下,这是我使用云功能的第一天。 I have a cloud function that returns an object to my client after verifying some details.我有一个云 function 在验证一些详细信息后向我的客户返回 object。 I also want to use this function to write to my firestore database.我还想使用这个 function 写入我的 Firestore 数据库。 But to write to my database i have to return a promise from my function.但是要写入我的数据库,我必须从我的 function 返回 promise。 How do i do both and make this work?我如何同时做到这一点并完成这项工作? Here's my code.这是我的代码。

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

    const chosenUsername = data.username;
    const chosenEmail = data.email;

    const allEmails: string[] = [];
    const allUsernames: string[] = [];

    let usernameExists = false;
    let emailExists = false;

    await admin.firestore().collection('Users').get()
        .then((snapshot) => {
            snapshot.forEach((doc) => {
                allEmails.push(doc.data().email);
                allUsernames.push(doc.data().username);
            })

            allEmails.forEach((email) => {
                if (email === chosenEmail) {
                    emailExists = true;
                }
            });

            allUsernames.forEach((username) => {
                if (username === chosenUsername) {
                    usernameExists = true;
                }
            });

            if (!usernameExists && !emailExists) {
                registerDetails(chosenUsername, chosenEmail);
            }

        })
        .catch((error) => {
            console.log('Error retrieving firestore documents', error);
        });

    return { usernameExists: usernameExists, emailExists: emailExists };

});


export const registerDetails = functions.https.onCall(async (data, context) => {
    return admin.firestore().collection('Users').add({
        email: data.chosenEmail,
        username: data.chosenUsername
    });
});

I tried using another https callback function and call it within the main function.我尝试使用另一个 https 回调 function 并在主 function 中调用它。 This doesn't write to my database but it does return my object.这不会写入我的数据库,但会返回我的 object。 Please help!请帮忙!

Factor out the contents of the registerDetails function so both onCall functions can use it...registerDetails function 的内容分解出来,以便两个 onCall 函数都可以使用它...

async function addUser(data) {
  return admin.firestore().collection('Users').add({
    email: data.chosenEmail,
    username: data.chosenUsername
  })
}

Now verifyDetails can use it like this...现在verifyDetails可以像这样使用它......

export const verifyDetails = functions.https.onCall(async (data, context) => {
  let querySnapshot = await admin.firestore().collection('Users').get()
  // code from the OP here.
  // because we used await, the code doesn't have to be in a then block
  
  await addUser({ chosenEmail, chosenUsername })
  return { usernameExists: usernameExists, emailExists: emailExists };
})

and registerDetails can use it like this...并且registerDetails可以像这样使用它...

export const registerDetails = functions.https.onCall(async (data, context) => {
  return addUser(data)
})

Note that verifyDetails doesn't return a promise, but it awaits the async work, which also works.请注意,verifyDetails 不会返回 promise,但它会等待异步工作,这也是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM