如何避免在Firebase云函数中嵌套承诺?

huangapple go评论83阅读模式
英文:

How to avoid nesting promises in Firebase Cloud Functions?

问题

I'm using this tutorial as a main way to learn about Dialogflow and Firebase, and I've gotten stuck in the following section of code:

25   if(action === 'firebase.update'){
26          let userId = 'marc.tuinier';
27          // Check if the user is in our DB
28          admin.firestore().collection('users').where('userId', '==', userId).limit(1).get()
29              .then(snapshot => {
30                  let user = snapshot.docs[0]
31                  if (!user) {
32                      // Add the user to DB
33                      admin.firestore().collection('users').add({
34                          userId: userId
35                      }).then(ref => {
36                          sendResponse('Added new user');
37                      });
38                  } else {
39                      // User in DB
40                      sendResponse('User already exists');
41                  }
42              });
43      }

I'm getting these errors:

 28:9   error    Expected catch() or return                  promise/catch-or-return
 31:17  error    Each then() should return a value or throw  promise/always-return
 33:21  error    Expected catch() or return                  promise/catch-or-return
 33:21  warning  Avoid nesting promises                      promise/no-nesting
 35:29  error    Each then() should return a value or throw  promise/always-return

I was predominantly wondering how to fix these errors (and perhaps some material so I can learn more about it - thanks in advance!)

英文:

I'm using this tutorial as a main way to learn about Dialogflow and Firebase, and I've gotten stuck in the following section of code:

25   if(action === 'firebase.update'){
26          let userId = 'marc.tuinier';
27          // Check if the user is in our DB
28          admin.firestore().collection('users').where('userId', '==', userId).limit(1).get()
29              .then(snapshot => {
30                  let user = snapshot.docs[0]
31                  if (!user) {
32                      // Add the user to DB
33                      admin.firestore().collection('users').add({
34                          userId: userId
35                      }).then(ref => {
36                          sendResponse('Added new user');
37                      });
38                  } else {
39                      // User in DB
40                      sendResponse('User already exists');
41                  }
42              });
43      }

I'm getting these errors:

 28:9   error    Expected catch() or return                  promise/catch-or-return
 31:17  error    Each then() should return a value or throw  promise/always-return
 33:21  error    Expected catch() or return                  promise/catch-or-return
 33:21  warning  Avoid nesting promises                      promise/no-nesting
 35:29  error    Each then() should return a value or throw  promise/always-return

I was predominantly wondering how to fix these errors (and perhaps some material so I can learn more about it - thanks in advance!)

答案1

得分: 2

关于避免嵌套的Promise,我建议你研究一下async/await,然后你可以得到如下的代码。然后,你可以添加try/catch块来进一步调试。

if (action === 'firebase.update') {
    let userId = 'marc.tuinier';
    // 检查用户是否在我们的数据库中
    let snapshot = await admin.firestore().collection('users').where('userId', '==', userId).limit(1).get()
    let user = snapshot.docs[0]
    if (!user) {
        // 将用户添加到数据库
        await admin.firestore().collection('users').add({
            userId: userId
        })
        return sendResponse('已添加新用户');
    } else {
        return sendResponse('用户已存在');
    }
}

希望这对你有所帮助。

英文:

So as far as avoiding nested promises I would recommend looking into async / await, leaving you with something like the following. You can then add try/catch blocks to debug further

if(action === 'firebase.update'){
       let userId = 'marc.tuinier';
       // Check if the user is in our DB
       let snapshot = await admin.firestore().collection('users').where('userId', '==', userId).limit(1).get()
       let user = snapshot.docs[0]
        if (!user) {
            // Add the user to DB
            await admin.firestore().collection('users').add({
                userId: userId
            })
            return sendResponse('Added new user');
        } else {
            return sendResponse('User already exists');
        }
   }

</details>



huangapple
  • 本文由 发表于 2020年1月3日 22:35:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定