I want to implement membership with Node.js and Mongodb.

Asked 2 years ago, Updated 2 years ago, 130 views

Hello, it's nothing but I'm working on a code to implement membership with node.js and mongoDB I have a question because I couldn't get the username and password continuously.
I don't know what the hell's wrong with two non-majors TT

First of all, the register.ejs file.

<body>
    <div class="center">
        <h1> Membership</h1>
        <form action= "/users/register" method="POST" enctype="multipart/form-data">

            <div class="txt_field">
                <input type="text" id="username" name="username" required>
                <span></span>
                <label>Username</label>
            </div>

            <div class="txt_field">
                <input type="password" id="password" name="password" required>
                <span></span>
                <label>Password</label>
            </div>

            <input type="submit" value="Register" id="sendMassageButton">

        </form>
    </div>
</body>

StoreUser.js.

const User = require('../models/User.js')
const path = require('path')

module.exports = (req,res) =>{
    User.create(req.body, (error, user)=>{
        res.redirect('/')
    })
}

This is User.js.

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema({
    username: String,
    password: String
});


//export model
const User = mongoose.model('User',UserSchema);

module.exports = User

Finally, index.js.

const express = require('express')
const app = new express()

const mongoose = require('mongoose');
const path = require('path')

const storeUserController = require('./controllers/storeUser')

mongoose.connect('mongodb://localhost/user_database',{
    useNewUrlParser: true,
    useUnifiedTopology: true});


const ejs = require('ejs')

app.set('view engine','ejs')
app.use(express.static('public'))

app.listen(4000, ()=>{
    console.log('App listening on port 4000')
})


app.get('/',(req,res)=>{
    res.render('index');
})

app.post('/users/register',storeUserController)

What's the problem?(Crying) If you press the register button, something is saved in mongoDB... It's empty...

I don't think I can get the username and password from the text box in register.ejs I'm so frustrated that I'm going crazy.

I made test.js separately and saved them randomly.
If you press the register button, only the last weird one will appear.
I think there's something wrong with the ejs. What's wrong with you? He's helping me.

node.js mongodb sign-up

2022-09-20 15:55

1 Answers

I don't really know express... It's because of him, with a high probability. Erase this and try again.

 enctype="multipart/form-data"

Based on the assumption, a basic POST form without it in an HTML form is sent in a application/x-www-form-lencoded meme type, and a form that needs to be "attached to a file" is sent in a multipart/form-data meme type, which is probably why the request is slightly different. Maybe that's why the content is "empty."

Anyway, try it and let me know if it doesn't work.


2022-09-20 15:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.