Answer the question
In order to leave comments, you need to log in
My NodeJS application is behaving very strangely, has anyone experienced this?
Good day, I decided to practice NodeJS and wanted to write a chat. Created a registration page, with data loading into MongoDB Atlas. By the way, here it is . The app itself is on Heroku.
Everything is OK on the local machine, everything is sent to the database, no errors and warnings, when I deployed to Heroku, everything was also OK, I was able to register from the computer. Then I opened the site on my phone (although it may just be a coincidence that lasts for 2 days), signed up and gives me a 503HTTP Error and these are the logs, I read them, but there is absolutely nothing useful there:
The whole application crashes, and with further attempts to register, it turns out everything is the same.
Here is the code for adding values to the database:
const MongoDB = new MongoClient("mongodb+srv://admin:тут-пароль@sunshine.k3eim.mongodb.net/sunshine-database?retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true}) // пароль от бд специально убрал, не ошибка
app.post("/register_me", (req, res) => {
const data = [req.body.name, req.body.login, req.body.password]
MongoDB.connect((err) => {
const collection = MongoDB.db("sunshine-database").collection("users")
collection.insertOne({name: data[0], login: data[1], password: data[2]})
.then(() => {
res.render('success', {
name: data[0]
})
MongoDB.close()
})
.catch((reason) => {
throw reason
})
})
})
Answer the question
In order to leave comments, you need to log in
Corrected, I created a flag variable that answered the question "Have you connected to the server?" true : false
After that, I transferred the connection to the method that starts immediately after the server starts (server.listen())
And in the method responsible for deploying to the database, I checked if the success variable is equal to true, otherwise I rendered an error message.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question