Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

node.js - API calls work locally but gives MongooseError in heroku

I've spend hours on this and I can't find what I'm doing wrong. Everything works well locally Here's my subdocuments

const linkSchema = new mongoose.Schema({
  tag: String,
  name: String,
  url: String,
});

const monthSchema = new mongoose.Schema({
  monthName: String,
  links: [linkSchema],
});

The models

const Link = mongoose.model("Link", linkSchema);
const Month = mongoose.model("Month", monthSchema);

And the HTTP requests. For the POST request, I'm getting some UnhandledPromiseRejectionWarning. I'm not sure why I'm getting this because I handled it using a catch block

app.post(`/${year}/`, (request, response) => {
  const linkToAdd = request.body;
  console.log("This is the body", linkToAdd);
  const link = new Link({
    tag: linkToAdd.tag,
    name: linkToAdd.name,
    url: linkToAdd.url,
  });
  const monthWhereYouAdd = linkToAdd.month;
  Month.findOne({ monthName: `${monthWhereYouAdd}` })
    .then((result) => {
      const linksArray = result;
      linksArray.links.push(link);
      return linksArray.save();
    })
    .then((result) => {
      console.log(result);
      response.status(200).end();
    })
    .catch((error) => next(error));
});
app.get(`/${year}/:month`, (request, response, next) => {
  Month.find({ monthName: `${request.params.month}` })
    .then((result) => {
      if (result.length > 0) {
        response.json(result);
      } else response.status(304).end();
    })
    .catch((error) => next(error));
});

When I made a GET request and checked the logs

2020-12-31T16:39:23.156866+00:00 heroku[router]: at=info method=GET path="/2020/January" host=linksforyou.herokuapp.com request_id=9db58a28-cff4-41f9-a88a-4950d1c80f13 fwd="183.83.172.109" dyno=web.1 connect=2ms service=10023ms status=500 bytes=436 protocol=https

2020-12-31T16:39:23.153796+00:00 app[web.1]: Operation `months.find()` buffering timed out after 10000ms

2020-12-31T16:39:23.156276+00:00 app[web.1]: MongooseError: Operation `months.find()` buffering timed out after 10000ms

2020-12-31T16:39:23.156278+00:00 app[web.1]:     at Timeout.<anonymous> (/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:184:20)

2020-12-31T16:39:23.156278+00:00 app[web.1]:     at listOnTimeout (internal/timers.js:554:17)

2020-12-31T16:39:23.156279+00:00 app[web.1]:     at processTimers (internal/timers.js:497:7)

Is months.find() referring to Month.findOne? Why do I not get this error locally?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Found the solution and the stupid mistake I was making. Heroku wasn't able to take config variables like MONGODB_URI and PORT, even though I had stored them using dotenv. I added them on their website and now its working!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...