New to Typescript. Wrote this function but I can’t figure out why it’s giving the error below. I commented the 2 lines that the errors are pointing to.
JavaScript
x
55
55
1
import * as functions from 'firebase-functions';
2
import * as admin from 'firebase-admin';
3
admin.initializeApp();
4
5
exports.createUser = functions.https.onCall(async (data, context) => {
6
console.log('_createUser: ');
7
const uid = context?.auth?.uid;
8
if (uid) {
9
const username = data.user;
10
const email = data.email;
11
12
//Check to see if that username already exists
13
const qData = await admin.firestore().collection('users').where('username', '==', username).limit(1).get();
14
qData.forEach(doc => {
15
const otherUsername = doc.get('username').toString();
16
17
if (otherUsername) {
18
console.log('_createUser: Username is already in use.');
19
return 'Username is already in use.'
20
}
21
else {
22
//Create collection for this user's friends list
23
const friendsColl = 'friends_' + uid;
24
const friendsDoc = admin.firestore().collection(friendsColl).doc();
25
friendsDoc.set({ //Error #1 is here
26
//Forces the collection to exist
27
exists: 1,
28
29
//Other useful data
30
createDate: admin.firestore.FieldValue.serverTimestamp(),
31
modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
32
ownerUsername: username,
33
ownerUID: uid, //
34
rowType: 'B', //N = normal, B = backend (created for server side reasons)
35
})
36
37
const userDoc = admin.firestore().collection('users').doc(uid); // use uid as document ID
38
userDoc.set({ //Error #2 is here
39
createDate: admin.firestore.FieldValue.serverTimestamp(),
40
modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
41
username: username,
42
email: email,
43
stat: 1, //0 = banned, 1 = normal
44
uid: uid,
45
friendsColl: friendsColl,
46
})
47
return console.log('_createUser_finished');
48
};
49
});
50
}
51
else {
52
return console.log('_createUser_Error: User is not authorized');
53
};
54
});
55
48:9 error Promises must be handled appropriately or explicitly marked as ignored with the
void
operator @typescript-eslint/no-floating-promises
61:9 error Promises must be handled appropriately or explicitly marked as ignored with the
void
operator @typescript-eslint/no-floating-promises
Advertisement
Answer
You need to use then with set method to return promise. Like this
JavaScript
1
12
12
1
friendsDoc.set({ //Error #1 is here
2
//Forces the collection to exist
3
exists: 1,
4
5
//Other useful data
6
createDate: admin.firestore.FieldValue.serverTimestamp(),
7
modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
8
ownerUsername: username,
9
ownerUID: uid, //
10
rowType: 'B', //N = normal, B = backend (created for server side reasons)
11
}).then(result => {});
12