0

I am working on user signup with nodejs and mongo db , problem is when i inserted all data into collection it's returning all the data in the document i want to restrict some of objects in returning data for example in below example in user object password in included which i should restrict ,

newUser =  new UserSchema({
   firstName : firstName,
   middleName : middleName,
   lastName : lastName,
   email : email,
   location : location,
   password : password,
   createdDate :new Date(Date.now()).toISOString(),
   role : "user"
})
password : { // in scehma
   type: String,
    select :false,
    hide: true,
    required:true
 },
newUser.save((err,user) => {
    if(err){
        return false;
    }else{
        return user ; // user object should not have password in this 
    }
});

3 Answers 3

2

Just put select:false with password field where you defined schema.

like :

password : {type : String, select : false}

so it will not return password filed.

Sign up to request clarification or add additional context in comments.

2 Comments

Still i am i getting password field , i just dropped my collection and restarted db server , still same results
Just drop indexes from that particular collection using db.collection.dropIndex() and start the server again. Now you are not able to get password field.
0

mongoose-hidden this plugin helped me , but anyway select:false should work , if not else after long try you can use above plugin

Comments

0

What you can do is before returning this user you can delete the password filed from that. Like below

newUser.save((err,user) => {
    if(err){
        return false;
    }else{
        user = user.toObject();
        delete user.password;
        return user ; // Now user object do not have password field 
    }
});

2 Comments

I have fields through out application which need not to be populated for all API calls
Then I think the best solution is to go with the answer given by @vaibhavpatil.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.