0

I wnader why await citiesRef.get(); in the exports.createID get error despite the same await citiesRef.get(); in the exports.newID doesn't.

exports.newID = functions.https.onRequest(async (req, res) => {
  let citiesRef = fireStore.collection('system').doc('uid_counter');
  let snapshot = await citiesRef.get();
  let counter = snapshot.get('counter');
  console.log('current counter = ', counter);
  res.status(200).send({ counter: counter});
  snapshot.ref.update({counter: coounter})
//  res.send('aho');
});

exports.createID = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
  // ... Your code here
  const newValue = snap.data();
  let citiesRef = fireStore.collection('system').doc('uid_counter');
  let snapshot = await citiesRef.get();
  let counter = snapshot.get('counter');
  let id = hashids.encode(counter++)
  snap.ref.update({id: id})
  console.log(id)
  counter 
});

enter image description here

Any suggestions are welcome! Thank you!

1 Answer 1

2

You need to add async keyword:

exports.createID = functions.firestore.document('users/{userId}').onCreate(async (snap, context) => {

The await operator is used to wait for a Promise. It can only be used inside an async function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.