Reputation: 819
I am trying to get user data from mongodb using get method but it continuously giving me the error that user.findOne
is not a function user token and id get successfully but findOne
is not working, I used static methods with user Schema. Please help me in this regard. thank you.
Here is the Code:
//userController.js
Router.get("/user",(req,res,next)=>{
var token=req.header("x-auth");
console.log("token3: "+token);
console.log(User);
User.findUserByToken(token).then((then)=>{
if(!User){
Promise.reject();
}else{
req.user=user;
req.token=token;
next();
}
}).catch((error)=>{
res.status(401).send();
});
},(req,res)=>{
res.send(req.user).then(()=>{
}).catch(()=>{
});
})
//user.js
const Schema=mongoose.Schema;
const userSchema=new Schema({
name:{
type: String,
trim: true,
required: true,
minlength: 6
},
email:{
type:String,
unique:true,
trim: true,
required: true,
minlength: 6
},
password:{
type: String,
trim: true,
required: true,
minlength: 8
},
tokens:[
{
access:{
type:String,
required:true
},
token:{
type:String,
required:true
}
}
]
});
userSchema.statics.findUserByToken=(token)=>{
const User= this;
console.log(User);
let decoded;
try{
decoded=jwt.verify(token,"asdfghjkl");
}catch(e){
return Promise.reject();
}
this.findOne({
"_id":decoded._id,
"tokens.token":token,
"tokens.access":"auth"
}).then((data)=>{
return data;
})
}
const User=mongoose.model('users',userSchema);
module.exports={User}
here is Error in Postman:
TypeError: this.findOne is not a function
at Function.userSchema.statics.findUserByToken (D:\react-native\reactNativeBackend\models\user.js:123:10)
at D:\react-native\reactNativeBackend\controllers\userController.js:47:10
at Layer.handle [as handle_request] (D:\react-native\reactNativeBackend\node_modules\express\lib\router\layer.js:95:5)
at next (D:\react-native\reactNativeBackend\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\react-native\reactNativeBackend\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\react-native\reactNativeBackend\node_modules\express\lib\router\layer.js:95:5)
at D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:335:12)
at next (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:174:3)
at router (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (D:\react-native\reactNativeBackend\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:317:13)
at D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:335:12)
at next (D:\react-native\reactNativeBackend\node_modules\express\lib\router\index.js:275:10)
Upvotes: 1
Views: 2582
Reputation: 2573
This was stated in the docs here:
Do not declare statics using ES6 arrow functions (=>). Arrow functions explicitly prevent binding this.
So you need to change the arrow function into a function expression:
userSchema.statics.findUserByToken = function (token) {
const User = this;
console.log(User);
let decoded;
try {
decoded = jwt.verify(token, "asdfghjkl");
} catch (e) {
return Promise.reject();
}
this.findOne({
"_id": decoded._id,
"tokens.token": token,
"tokens.access": "auth"
}).then((data) => {
return data;
})
}
Upvotes: 5