Arthur
Arthur

Reputation: 21

RESOLVED I cant save the Post var in the MongoDB because my User.findOne(email) doesnt work

I'm setting up a website backend. When i run the register from the post my User.findOne(email) give this error message:

(node:10052) UnhandledPromiseRejectionWarning: ObjectParameterError: Parameter "filter" to findOne() must be an object, got [email protected] at new ObjectParameterError (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\mongoose\lib\error\objectParameter.js:25:11) at model.Query.Query.findOne (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\mongoose\lib\query.js:2135:16) at Function.findOne (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\mongoose\lib\model.js:2132:13) at D:\Arquivo de Programas\Projetos VS Code\devbook\backend\src\routes.js:40:27 at Layer.handle [as handle_request] (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\layer.js:95:5) at next (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\layer.js:95:5) at D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:281:22 at Function.process_params (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:335:12) at next (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:275:10) at Function.handle (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:174:3) at router (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:317:13) at D:\Arquivo de Programas\Projetos VS Code\devbook\backend\node_modules\express\lib\router\index.js:284:7 (node:10052) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:10052) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I already change the imsomnia id's, the parameter from the _id to user and the user from the email.

My routes.js:

const routes = require('express').Router();
const bcrypt = require('bcryptjs');

const User = require('./models/UserModel');
const Post = require('./controllers/PostController');

routes.get('/', (req, res) => {
  return res.send('Hello, World!')
});

routes.post('/register', async (req, res) => {
  try {
    const user = await User.create(req.body);

    return res.send({ user })
  } catch {
    return res.status(400).send({ error: 'Registration failed' })
  }
});

routes.post('/auth', async (req, res) => {
  const { email, password } = req.body;

  const user = await User.findOne({ email }).select('+password');

  if (!user){
    return res.status(400).send({ error: 'User not found' });
  };

  if (!await bcrypt.compare(password, user.password)){
    return res.status(400).send({ error: 'Invalid password '});
  };

  res.send({ user });
})

routes.post('/post', async (req, res) => {
  const { text, email } = req.body //only test 

  const user = await User.findOne(email)

  if (!user){
    return res.status(400).send({ error: 'User not found' });
    }
    if (!text){
      return res.status(400).send({ error: 'Text not found' });
    }

  res.send(text)
});

module.exports = routes;

I expect the register from the post to show it after in the homepage

SOLUTION:

turn the const user = await User.findOne(email) to const user = await User.findOne({ email: email })

Upvotes: 0

Views: 177

Answers (1)

djs
djs

Reputation: 4065

From the error message it seems like the call to findOne() is getting just the email string and not an object. Why don't you use the same syntax you did in the other routes?

You wrote this in the /post route:

  const user = await User.findOne(email)

But it seems like you should write this:

  const user = await User.findOne({ email } )

Correct? Here is the top half of the route for reference, with the proposed correction:

routes.post('/post', async (req, res) => {
  const { text, email } = req.body //only test 

  const user = await User.findOne({ email })

Upvotes: 0

Related Questions