A
A
Artyom2020-03-23 19:56:05
JavaScript
Artyom, 2020-03-23 19:56:05

Why doesn't it add mongodb to the database?

Hello everyone, I'm using the mongoose library!
I use the documentation, but after entering the data from the form, I have a load and last forever, and the data is not added to the database!
Here is the routing code (using express js)

var express = require('express')
var body_parser = require('body-parser')
var path = require('path')
const nodemailer = require('nodemailer')
const mongoose = require('mongoose')

var Schema = mongoose.Schema

var blogSchema = new Schema({
    login: String,
    email: String,
    password: String
  });

var Reg = mongoose.model('Blog', blogSchema);

var app = express()

//дальше идет подключение к бд

//Ну и сам роутинг
app.get('/auth', (req, res) => {
    res.render('auth', {title: "Авторизация"})
})

app.post('/auth', (req, res) => {
    console.log(req.body)
})

app.get('/register', async (req, res) => {
    const register = await Reg.find({})
    res.render('register', {
        title: "Регистрация",
        register
    })
})

app.post('/register', async (req, res) => {
    const register = new Reg({
        login: req.body.login,
        email: req.body.email,
        password: req.body.password
    })

    await register.save()
})


app.listen(3012, () => {
    console.log('Server is started')
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-03-23
@hzzzzl

I'm loading and taking forever

well, it's because app.post('/register' returns nothing, try at least res.sendStatus(200) at the end of the route, then the browser will get the response to the request
does not add data to database

Yes, it seems to work, does not write any error to the console at the same time?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question