CodeGenieApp / CodeGenieApp/serverless-express
How to use Async/await?
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 676
- PR merge metrics
- No merged PRs in 30d
Description
I read https://github.com/vendia/serverless-express/issues/481 https://github.com/vendia/serverless-express/issues/134 and https://github.com/vendia/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/src/lambda-async-setup.js but I still cannot figure out how to connect to a database (postgres, not from RDS). A minimal example of what I'm trying to do:
```
//lambda.js
require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')
exports.handler = serverlessExpress({ app })
//app.js
require('dotenv').config();
const { Client } = require('pg') //leaving out the rest of the libraries
router.post('/createUser', async function (req, res) {
await sqlRequest(`INSERT INTO users ("name", "age" "ect..") VALUES ( ${req.body.name}, ${req.body.age}, ${req.body.ect..})`);
res.status(200).send("inserted!");
})
router.post('/getUser', async function (req, res, next) {
let result = sqlRequest('SELECT * FROM users WHERE id = ' + req.body.id) //all user input is escaped in full version
res.status(200).json(result);
})
async function sqlRequest(sql, params){
const client = new Client(process.env.DATABASE_URL); //this is a postgres URL
await client.connect();
let result = await client.query(sql);
client.end();
return result.rows;
}
module.exports = app
```
The full app is a few hundred lines with many more queries and works fine locally. I believe the issue is being caused by async/await. Is there any way to do this in lambda? If this isn't the right library to use, can someone point me in the right direction? Would you recommend I switch to Azure or some other service to make it easier? Any advice on how to proceed is appreciated.
Contributor guide
Assessment
This issue has not been assessed yet.