mikeurrestadev
mikeurrestadev

Reputation: 33

Heroku Error Code H12 problem with Node.JS, backend with MongoDB atlas

Problem:

I got this error when checking Heroku logs :

code=H12 desc="Request timeout" method=POST path="/delete" host=dry-falls-65221.herokuapp.com request_id=0d934a3c-84ea-4e7d-aa53-f437830b807e fwd="136.36.41.157" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https.

What I've done:

I checked the documentation and installed the whole thing. Check the app here . However, every time I try to delete an item from the list, the app crashes giving the H12 error. I checked the database which is in Mongo DB Atlas and the item got deleted, so its the page that is not redirecting properly. You can see that when you manually reload the page, it renders accordingly. How can I fix that issue?

Code section using NodeJS , also Express, body-parser, mongoose, lodash:

app.post("/delete", function(req, res) {
  const checkedItemId = req.body.checkbox;
  const listName = req.body.listName;
if (listName === "Today") {

  Item.findByIdAndRemove(checkedItemId, function(err) {
    if (err) {
      console.log(err);
    } else {
      console.log("Deleted!");
    }
  });
} else {

List.findOneAndUpdate({name:listName},{ $pull: {items: {_id: checkedItemId}}}, function (err, foundList) {

  if(!err) {

    res.redirect("/" + listName);
  }
});

}

});

I also attached the HTML section (EJS utilized)

  <% newListItems.forEach(function(item) { %>
<form class="item" action="/delete" method="post">
  <div class="item">
    <input  name="checkbox" value="<%=item._id%>" type="checkbox" onChange="this.form.submit()">
    <p><%=  item.name  %></p>
  </div>
  <input type="hidden" name="listName" value="<%= listTitle %>">
</form>

Any help would be really appreciated.

Upvotes: 2

Views: 600

Answers (2)

L. Theodore Obonye
L. Theodore Obonye

Reputation: 1340

The reason you are experiencing this error might be because the DB is not allowing you to make any changes. I recommend the following to catch the error:

  1. check the logs
heroku logs --tail

look for an ip address for each attempt at consuming an api endpoint.

E.g enter image description here

I tried to hight the IP address, it's not showing. look for fwd="102.65.57.87" as an example then add it to your allowed IPs in the network access section.

If allowing the IPs doesn't work, try this:

try {
  mongoose.connect(db, {
    useUnifiedTopology: true,
    useNewUrlParser: true
  });
  console.log('MongoDB is Connected...');
} catch (err) {
  console.error(err);
  process.exit(1);
}

If there is an error with your DB you will get information for the code above and you can troubleshoot better.

Happy coding!!!

Upvotes: 0

RUGVED
RUGVED

Reputation: 182

I had similar error.
I found a link https://dev.to/cpclark360/how-to-host-a-restful-node-js-server-with-mongodb-atlas-database-on-heroku-1opl. Refer this link ,it has entire set up process for mongodb atlas and node js.
Most probably you forgot to set environment variables in heroku.
Hope this helps :)

Upvotes: 1

Related Questions