简体   繁体   中英

Cannot get response from express endpoint

I'm building an API that I intend to use with my react native application. The problem which I'm facing right now is that I when I try to hit this particular route /api/auth/signup in Postman I get the Could not get any response error message.

This is the route:

//create user token
router.post(
  "/signup",
  [check("username").isEmail(), check("password").isLength({ min: 6 })],
  async (req, res) => {
    //validate input field on the backend
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(422).json({ errors: errors.array() });
    }

    const { username, password, firstName, lastName } = req.body;

    try {
      //search DB if there is an existing user
      let user = await User.findOne({ username });

      if (user) {
        return res.status(400).json({ msg: "User already exists" });
      }

      const salt = await bcrypt.genSalt(10);

      res.status(200).json({ data: salt });
    } catch (error) {}
  }
);

module.exports = router;

The strange thing is that if I remove the User.findOne function I get a response. I don't know why this keeps happening, as I had built a similar application following the same pattern without a problem.

NOTE: In the main app.js I have the app.use(express.json({extended:true}) , I've also successfully linked the routes in the main file too. Any help would be greatly appreciated!

I think you have a typo in your app.use... statement which you've given in your comment as,

app.use(express.json({extented:true})

which needs to be corrected as, ( 'd' should come in place of 't' )

app.use(express.json({extended:true})

Hope this helps!

So it turns out when I was requiring mongoose in one of my models I required it with an uppercase letter const mongoose = require('Mongoose') which isn't valid, therefore the findOne method won't run because there's no model to take it from. Strange that visual studio code didn't complain about the false import. Anyways, thanks to anyone willing to help me out :) !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM