sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Unable to create new user #193

Closed megwelch closed 1 year ago

megwelch commented 1 year ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

MERN

What's the problem you're trying to solve?

Unable to create a new user

Post any code you think might be relevant (one fenced block per file)

// SIGN UP
// POST /sign-up
router.post('/sign-up', (req, res, next) => {
    // start a promise chain, so that any errors will pass to `handle`
    Promise.resolve(req.body.credentials)
        // reject any requests where `credentials.password` is not present, or where
        // the password is an empty string
        .then((credentials) => {
            if (
                !credentials ||
                !credentials.password ||
                credentials.password !== credentials.password_confirmation
            ) {
                throw new BadParamsError()
            }
        })
        // generate a hash from the provided password, returning a promise
        .then(() => bcrypt.hash(req.body.credentials.password, bcryptSaltRounds))
        .then((hash) => {
            // return necessary params to create a user
            return {
                email: req.body.credentials.email,
                hashedPassword: hash,
            }
        })
        // create user with provided email and hashed password
        .then((user) => User.create(user))
        // send the new user object back with status 201, but `hashedPassword`
        // won't be send because of the `transform` in the User model
        .then((user) => res.status(201).json({ user: user.toObject() }))
        // pass any errors along to the error handler
        .catch(next)
})

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

 09:31:16 GMT-0500 (Eastern Standard Time):
MongoServerError: E11000 duplicate key error collection: express-auth-template-development.users index: username_1 dup key: { username: null }
    at /Users/megwelch/sei/projects/project-4/project-4-API/node_modules/mongodb/lib/operations/insert.js:51:33
    at /Users/megwelch/sei/projects/project-4/project-4-API/node_modules/mongodb/lib/cmap/connection_pool.js:272:25
    at handleOperationResult (/Users/megwelch/sei/projects/project-4/project-4-API/node_modules/mongodb/lib/sdam/server.js:370:9)
    at MessageStream.messageHandler (/Users/megwelch/sei/projects/project-4/project-4-API/node_modules/mongodb/lib/cmap/connection.js:479:9)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/Users/megwelch/sei/projects/project-4/project-4-API/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
    at MessageStream._write (/Users/megwelch/sei/projects/project-4/project-4-API/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10) {
  index: 0,
  code: 11000,
  keyPattern: { username: 1 },
  keyValue: { username: null }
}

What is your best guess as to the source of the problem?

From the error, a duplicate, however, I am confused because it's also saying 'username: null' when my log in credentials are email and not username

What things have you already tried to solve the problem?

I haven't touched the user model or routes on the back end since they came with the boiler plate so I'm honestly really confused why this is happening. I've tried searching the back-end for anywhere that it says username and there is no where that it says username except in a message.

Paste a link to your repository here https://github.com/megwelch/Project-4_Back-End https://github.com/megwelch/Project-4_Front-End

timmshinbone commented 1 year ago

it'll be easier to explain in your breakout room, I'll be there in a sec

megwelch commented 1 year ago

Solution: Forgot to change the name of the database in the back-end config file