R
R
Rag'n' Code Man2020-07-19 22:47:34
Express.js
Rag'n' Code Man, 2020-07-19 22:47:34

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: 5f14a20f02fa2558490531.jpeg

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
        })
    })
})


Connection to my MongoDB Atlas cluster is available from any IP.

PS: Application survives only one request to DB and crashes

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2020-07-20
@iDmitriyWinX

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 question

Ask a Question

731 491 924 answers to any question