英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论