问题描述
我知道有很多这个问题的版本,但我找不到任何帮助我。
函数isAuthenticated(){
返回撰写()
//验证智威汤逊
。使用(功能(REQ,资源,下一个){
//允许的access_token通过查询参数传递,以及
如果(req.query&放大器;&放大器; req.query.hasOwnProperty('ACCESS_TOKEN')){
req.headers.authorization ='承载'+ req.query.access_token;
}
validateJwt(REQ,资源,下一个);
})
//连接用户的请求
。使用(功能(REQ,资源,下一个){
User.findById(req.user._id,函数(ERR,用户){
的console.log(下);
如果(ERR){
返回下一个(ERR);
}
如果(!用户){
返回res.send(401);
}
req.user =用户;
下一个();
});
});
}
以下是错误:
CastError:转换的ObjectId路径为_id失败值我
在ObjectId.cast(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\猫鼬\\ lib目录\\
架构\\ objectid.js:116:13)
在ObjectId.castForQuery(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\蒙戈
OSE \\ LIB \\架构\\ objectid.js:165:17)
在Query.cast(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\猫鼬\\ LIB \\阙
ry.js:2317:32)
在Query.findOne(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\猫鼬\\ lib目录\\
query.js:1118:10)
在Function.findOne(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\猫鼬\\升
IB \\ model.js:1049:13)
在Function.findById(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\猫鼬\\
LIB \\ model.js:986:15)
在Object.exports.show [如手柄(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\服务器\\一
PI \\ user.js的:43:8)
在next_layer(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\ EX preSS \\ LIB \\狂胜
呃\\ route.js:103:13)
在明(C:\\用户\\伯努瓦\\桌面\\ Gakusei \\ node_modules \\组合的中间件\\
LIB \\组合的-middleware.js:40:9)
在无极<&匿名GT; (C:\\用户\\伯努瓦\\桌面\\ Gakusei \\服务器\\权威性\\ auth.ser
vice.js:37:9)
我适应了一些文书上士样板AUTH code和我没有触及这部分。任何线索将不胜AP preciated。
有关completness,我在这里的API,用于我
exports.me =功能(REQ,资源,下一个){
VAR用户id = req.user._id;
User.findOne({
_id:用户id
},'-salt -hashedPassword',函数(ERR,用户){
如果(ERR){
返回下一个(ERR);
}
如果(!用户){
返回res.json(401);
}
res.json(用户);
});
};
您正在尝试通过查询_id访问与 findOne
一个对象,等于我。猫鼬是试图将字符串'我'转换成的ObjectId,但失败。我无法从回溯告诉我们,如果这是你的我
函数是问题(这是只有在findOne在$ C $用c您提供的地方)但你可以尝试改变函数调用 User.findById(用户ID,...)
来代替。不明白为什么用户id等于'我'但是如果这将有助于任何人在所有。希望这至少给了你一些方向。添加评论,如果你仍然有问题。
I know there are a lot of version of this question, but I couldn't find anything helping me.
function isAuthenticated() {
return compose()
// Validate jwt
.use(function (req, res, next) {
// allow access_token to be passed through query parameter as well
if (req.query && req.query.hasOwnProperty('access_token')) {
req.headers.authorization = 'Bearer ' + req.query.access_token;
}
validateJwt(req, res, next);
})
// Attach user to request
.use(function (req, res, next) {
User.findById(req.user._id, function (err, user) {
console.log(next);
if (err) {
return next(err);
}
if (!user) {
return res.send(401);
}
req.user = user;
next();
});
});
}
Here is the error :
CastError: Cast to ObjectId failed for value "me" at path "_id"
at ObjectId.cast (C:\Users\Benoit\Desktop\Gakusei\node_modules\mongoose\lib\
schema\objectid.js:116:13)
at ObjectId.castForQuery (C:\Users\Benoit\Desktop\Gakusei\node_modules\mongo
ose\lib\schema\objectid.js:165:17)
at Query.cast (C:\Users\Benoit\Desktop\Gakusei\node_modules\mongoose\lib\que
ry.js:2317:32)
at Query.findOne (C:\Users\Benoit\Desktop\Gakusei\node_modules\mongoose\lib\
query.js:1118:10)
at Function.findOne (C:\Users\Benoit\Desktop\Gakusei\node_modules\mongoose\l
ib\model.js:1049:13)
at Function.findById (C:\Users\Benoit\Desktop\Gakusei\node_modules\mongoose\
lib\model.js:986:15)
at Object.exports.show [as handle] (C:\Users\Benoit\Desktop\Gakusei\server\a
pi\user.js:43:8)
at next_layer (C:\Users\Benoit\Desktop\Gakusei\node_modules\express\lib\rout
er\route.js:103:13)
at next (C:\Users\Benoit\Desktop\Gakusei\node_modules\composable-middleware\
lib\composable-middleware.js:40:9)
at Promise.<anonymous> (C:\Users\Benoit\Desktop\Gakusei\server\auth\auth.ser
vice.js:37:9)
I'm adapting some of Yeoman boilerplate auth code and I didn't touch this part. Any clues would be greatly appreciated.
for completness, here my api for 'me'
exports.me = function (req, res, next) {
var userId = req.user._id;
User.findOne({
_id: userId
}, '-salt -hashedPassword', function (err, user) {
if (err) {
return next(err);
}
if (!user) {
return res.json(401);
}
res.json(user);
});
};
You are trying to access a object with findOne
by querying _id to be equal to 'me'. Mongoose is attempt to convert the string 'me' to an ObjectId but fails. I can't tell from the traceback if it's your me
function which is the issue (that's the only place where findOne is used in the code you have provided), but you could try to change the function call to User.findById(userId, ...)
instead. Don't see why userId is equal to 'me' however or if that would help any at all. Hope this gave you some direction at least. Add a comment if you still have problems.
这篇关于猫鼬:CastError:转换的ObjectId路径为“_id”失败值“我”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!