[英]Cannot get response from express endpoint
I'm building an API that I intend to use with my react native application.我正在构建一个我打算与我的本机应用程序一起使用的 API。 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.我现在面临的问题是,当我尝试在
Postman
此特定路由/api/auth/signup
,我收到无法获得任何响应错误消息。
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.奇怪的是,如果我删除
User.findOne
函数,我会得到响应。 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!注意:在主 app.js 中,我有
app.use(express.json({extended:true})
,我也成功地链接了主文件中的路由。任何帮助将不胜感激!
I think you have a typo in your app.use...
statement which you've given in your comment as,我认为您的
app.use...
声明中有一个错字,您在评论中给出了该声明,
app.use(express.json({extented:true})
which needs to be corrected as, ( 'd'
should come in place of 't'
)需要更正为,(
'd'
应该代替'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.所以事实证明,当我在我的一个模型中需要 mongoose 时,我需要一个大写字母
const mongoose = require('Mongoose')
这是无效的,因此 findOne 方法将不会运行,因为没有模型可以采用它从。 Strange that visual studio code didn't complain about the false import.奇怪的是,visual studio 代码并没有抱怨错误导入。 Anyways, thanks to anyone willing to help me out :) !
无论如何,感谢任何愿意帮助我的人:)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.