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!)
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');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With