December 2018
Intermediate to advanced
642 pages
15h 5m
English
Let's finish by working with a more complex example—a part of the REST code that worked with regions, which requires a database and uses promises, among several complications. Let's take the DELETE method handler as an example:
const deleteRegion = async ( res: any, dbConn: any, country: string, region: string) => { try { const sqlCities = ` SELECT 1 FROM cities WHERE countryCode="${country}" AND regionCode="${region}" LIMIT 1 `; const cities = await dbConn.query(sqlCities); if (cities.length > 0) { res.status(405).send("Cannot delete a region with cities"); } else { const deleteRegion = ` DELETE FROM regions WHERE countryCode="${country}" AND regionCode="${region}" `; const result = await dbConn.query(deleteRegion); ...Read now
Unlock full access