Skip to content

Instantly share code, notes, and snippets.

@CodingDoug
Last active November 6, 2022 09:29
Show Gist options
  • Save CodingDoug/814a75ff55d5a3f951f8a7df3979636a to your computer and use it in GitHub Desktop.
Save CodingDoug/814a75ff55d5a3f951f8a7df3979636a to your computer and use it in GitHub Desktop.
Example code from the video "Use async/await with TypeScript in Cloud Functions"

Example code from the video "Use async/await with TypeScript in Cloud Functions"

This is the example code from my video about using async/await with Cloud Functions. I've placed it here in a gist so it's easier to compare the "before" and "after" states for each case.

Watch the video here

The code in this project is licensed under the Apache License 2.0.

Copyright 2018 Google LLC
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    https://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
export const getBostonWeather = functions.https.onRequest((request, response) => {
admin.firestore().doc("cities-weather/boston-ma-us").get()
.then(snapshot => {
const data = snapshot.data()
response.send(data)
})
.catch(error => {
// Handle the error
console.log(error)
response.status(500).send(error)
})
})
export const getBostonWeather = functions.https.onRequest(async (request, response) => {
try {
const snapshot = await admin.firestore().doc("cities-weather/boston-ma-us").get()
const data = snapshot.data()
response.send(data)
}
catch (error) {
// Handle the error
console.log(error)
response.status(500).send(error)
}
})
export const getBostonAreaWeather = functions.https.onRequest((request, response) => {
admin.firestore().doc("areas/greater-boston").get()
.then(areaSnapshot => {
const cities = areaSnapshot.data().cities
const promises = []
cities.forEach(city => {
const p = admin.firestore().doc(`cities-weather/${city}`).get()
promises.push(p)
})
return Promise.all(promises)
})
.then(snapshots => {
const results = []
snapshots.forEach(snap => {
const data = snap.data()
data.city = snap.id
results.push(data)
})
response.send(results)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
})
export const getBostonAreaWeather = functions.https.onRequest(async (request, response) => {
try {
const areaSnapshot = await admin.firestore().doc("areas/greater-boston").get()
const cities = areaSnapshot.data().cities
const promises = []
cities.forEach(city => {
const p = admin.firestore().doc(`cities-weather/${city}`).get()
promises.push(p)
})
const snapshots = await Promise.all(promises)
const results = []
snapshots.forEach(snap => {
const data = snap.data()
data.city = snap.id
results.push(data)
})
response.send(results)
}
catch (error) {
console.error(error)
response.status(500).send(error)
}
})
@vinod-hosamani
Copy link

here is my error

src/index.ts:17:24 - error TS2532: Object is possibly 'undefined'.

17 const cities = areaSnapshot.data().cities
~~~~~~~~~~~~~~~~~~~

src/index.ts:18:15 - error TS7034: Variable 'promises' implicitly has type 'any[]' in some locations where its type cannot be determined.

18 const promises = []
~~~~~~~~

src/index.ts:19:24 - error TS7006: Parameter 'city' implicitly has an 'any' type.

19 cities.forEach(city => {
~~~~

src/index.ts:24:45 - error TS7005: Variable 'promises' implicitly has an 'any[]' type.

24 const snapshots = await Promise.all(promises)
~~~~~~~~

src/index.ts:26:15 - error TS7034: Variable 'results' implicitly has type 'any[]' in some locations where its type cannot be determined.

26 const results = []
~~~~~~~

src/index.ts:33:23 - error TS7005: Variable 'results' implicitly has an 'any[]' type.

33 response.send(results)
~~~~~~~

Found 6 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: tsc
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@ralbertini
Copy link

to ignore errors like ' Object is possibly 'undefined'.' :
go to tsconfig.json and add "strictNullChecks":false

@tkhduracell
Copy link

I'm missing examples with pubsub topics.

@BoHuang2018
Copy link

BoHuang2018 commented Apr 20, 2020

@vinod-hosamani @tkhduracell
I had the same errors with you, and fixed them with the following steps.

Step 1. To configure the file "functions/tsconfig.json" :
disable the line : "strict": true, ( in compilerOptions {...} )
enable a new option: "noImplicitAny": false (in compilerOptions {...} )
add a new line: "strictNullChecks":false. ( after "include": ["src"],)

Step 2. In the command line, run "npm install -g tslint"

Step 3. in the command line, run "npm run-script build "

After the step 1, the new file "functions/tsconfig.json" should be like
{ "compilerOptions": { "module": "commonjs", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, // "strict": true, "target": "es2017", "noImplicitAny": false }, "compileOnSave": true, "include": [ "src" ], "strictNullChecks":false }

@brikiernan
Copy link

@vinod-hosamani @tkhduracell
I had the same errors with you, and fixed them with the following steps.

Step 1. To configure the file "functions/tsconfig.json" :
disable the line : "strict": true, ( in compilerOptions {...} )
enable a new option: "noImplicitAny": false (in compilerOptions {...} )
add a new line: "strictNullChecks":false. ( after "include": ["src"],)

Step 2. In the command line, run "npm install -g tslint"

Step 3. in the command line, run "npm run-script build "

After the step 1, the new file "functions/tsconfig.json" should be like
{ "compilerOptions": { "module": "commonjs", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, // "strict": true, "target": "es2017", "noImplicitAny": false }, "compileOnSave": true, "include": [ "src" ], "strictNullChecks":false }

@BoHuang2018 This worked for me!! 60 mins of fussing and just needed a config tweak... lol

@knifewine
Copy link

Just wanted to say I stumbled on this code and it was a great help to unblock me in a project. Thank you! 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment