【发布时间】:2019-09-08 21:20:01
【问题描述】:
// @route GET api/profile/handle/:handle
// @desc Get profile by handle
// @access Public
router.get('/handle/:handle', (req, res) => {
const errors = {};
Profile.findOne({ handle: req.params.handle })
.populate('user', ['name', 'avatar'])
.then(profile => {
//console.log('profile1 ' + profile);
if (!profile) {
errors.noprofile = 'There is no profile for this user for handle route (from then block)';
res.status(404).json(errors);
}
res.json(profile);
})
.catch(err => res.status(404).json({ profile: 'There is no profile for this user for handle route (from error block)' }));
});
// @route GET api/profile/user/:user_id
// @desc Get profile by user ID
// @access Public
router.get('/user/:user_id', (req, res) => {
const errors = {};
Profile.findOne({ user: req.params.user_id })
.populate('user', ['name', 'avatar'])
.then(profile => {
// console.log('profile not found by userid');
//console.log('profile2 ' + profile);
if (!profile) {
errors.noprofile = 'There is no profile for this user for user_id route (from then block)';
res.status(404).json(errors);
}
res.json(profile);
})
.catch(err => res.status(404).json({ profile: 'There is no profile for this user for user_id route (from error block)',
err: err }));
});
我有上述两条路线。第一个是使用句柄(用户名)从 dB 中搜索用户,第二个是使用 dB 本身创建的 user_id 进行搜索。当我使用错误的句柄请求第一条路由时, then() 块被执行并且我得到了这个响应:
{
"noprofile": "There is no profile for this user for handle route (from then block)"
}
但是在第二条路线中(通过 user_id 搜索),当我输入错误的 user_id 时,catch 块被执行并且我得到了这个响应:
{
"profile": "There is no profile for this user for user_id route (from error block)",
"err": {
"message": "Cast to ObjectId failed for value \"5cb0ec06d1d6f93c20874427rhdh\" at path \"user\" for model \"profile\"",
"name": "CastError",
"stringValue": "\"5cb0ec06d1d6f93c20874427rhdh\"",
"kind": "ObjectId",
"value": "5cb0ec06d1d6f93c20874427rhdh",
"path": "user"
}
}
两条路线的逻辑相同,但它们的响应不同。这背后的原因是什么???
如果你想看看 Profile 架构,这里是:
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
handle: {
type: String,
required: true,
max: 40
},
company: {
type: String
},
....
....
.....
});
我在请求错误句柄时也收到了警告,如下所示:
(node:16996) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (H:\MERN Stack Course\devConnector\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (H:\MERN Stack Course\devConnector\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (H:\MERN Stack Course\devConnector\node_modules\express\lib\response.js:267:15)
at Profile.findOne.populate.then.catch.err (H:\MERN Stack Course\devConnector\routes\api\profile.js:75:39)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:16996) 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:16996) [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.
【问题讨论】:
标签: node.js mongodb express jwt passport.js