Updating MongoDB with mongoose and lambda-functions

Site Name - https://selftaught-dev.com/ (happy-hugle-97373a)

I’m trying to create a new user from my signup page (https://selftaught-dev.com/sign-up/) and add the user to a MongoDB instance with a lambda-function (https://github.com/ghughes13/Portfolio-v2/blob/master/src/functions/newUser.js) but every time I submit I get a 502 from the server.

When I look in the logs I can see that there was a timeout error.

4:28:47 PM: 2020-09-14T21:28:47.840Z	d9efa9ca-9d51-40cc-ab3c-e910182aafac	INFO	No Error; Trying to save to DB
4:28:57 PM: Duration: 10010.17 ms	Memory Usage: 84 MB	Init Duration: 278.68 ms	
4:28:57 PM: 2020-09-14T21:28:57.843Z d9efa9ca-9d51-40cc-ab3c-e910182aafac Task timed out after 10.01 seconds

It logs “No Error; Trying to save to DB” which is right before the ‘save’ function:

  console.log("No Error; Trying to save to DB")

  newUser.save(err => {
    if (err) {
      console.error(err)
    } else {
      console.log("saved data!")
    }
  })

so I believe it’s either not getting a response back from the DB (which it’s on MongoDB Atlas Database | Multi-Cloud Database Service | MongoDB) or it’s not even posting to the DB.

I’ve been able to get it to post with node.js locally, but can’t figure it out when I try to convert it to serverless. I know I’m probably just doing something dumb, so if anyone could shed some light on what it is, that’d be appreciated.

Also, I know I’m putting ‘sensitive’ info on git. I’m just playing around/trying to learn right now. There’s nothing in the DB right now and once I get it working I’ll delete the DB and start from scratch.

@ghughes13,

Firstly, welcome to the community!

Regarding your function – I’ve not used mongoose in lambda-function before so pardon if this question is obvious. Is the newUserModel.save() call returning a promise? If so, have you considered making your function async?

That said, maybe adding some more console.logs to give you a better idea on if the function was able to connect to your database would be useful.

Did you ever figure out the timeout error? I’m getting the same issue on the line of code that saves the doucment to MongoDB. Seems like the save alone is taking around 6 seconds. The rest of my function can finish in around 4.

What location is your MongoDB instance located in? Maybe you can read this guide and debug a bit: