Reputation: 2276
When a new user is created, I want to have a cloud function insert a new record into my users
collection. I tried doing so with the code below but when I checked my logs it gave this error:
TypeError: Cannot read property 'resourcePath' of undefined at doc (/workspace/node_modules...)
The reason I want to do it this way is because I want to be able to store the document ID within the document itself. This will serve as a "rowpointer" like in SQL - a unique way to identify the row in the collection.
Code:
exports.newUserSignup = functions.auth.user().onCreate(user => {
console.log('user created', user.email, user.uid);
const doc = admin.firestore().collection('users').doc
return doc(user.uid).set({
createDate: admin.database.ServerValue.TIMESTAMP,
modifiedDate: admin.database.ServerValue.TIMESTAMP,
username: 'blah',
email: user.email,
stat: 1,
uid: user.uid,
rowpointer: doc().id,
});
});
Upvotes: 0
Views: 129
Reputation: 598847
You're almost there, but have a few syntax errors in your code. This should be closer:
exports.newUserSignup = functions.auth.user().onCreate(user => {
console.log('user created', user.email, user.uid);
const doc = admin.firestore().collection('users').doc();
return doc.set({
createDate: admin.database.ServerValue.TIMESTAMP,
modifiedDate: admin.database.ServerValue.TIMESTAMP,
username: 'blah',
email: user.email,
stat: 1,
uid: user.uid,
rowpointer: doc.id,
});
});
I highly recommend keeping the reference docs handy when you're having trouble with this type of exercise, as I found the problem pretty quickly when looking at CollectionReference.doc()
and DocumentReference
Upvotes: 3