Need someone to explain what a highlighted line doing

code for user model

const mongoose=require('mongoose');
const validator=require('validator');
const bcrypt=require('bcrypt');
const jwt=require('jsonwebtoken');
const userSchema=new mongoose.Schema({
    name:{
        type:String,
        required:true,// making  name compulsory to be entered 
        trim:true // trim will remove spaces
    },
    email:{
        type:String,
        unique:true,
        validate(value){
            if(!validator.isEmail(value)){
                throw new Error("Please enter email");
            }
        }
    },   
    age:{
        type:Number,
        // age is not compulsory 
        validate(value){// validating age should be greater than 0
            if(value<0){
                throw new Error("Age should be greater than 0");
            }
        }
    },
    password:{
        type:String,
        trim:true,
        required:true,
        minlength:7,
        validate(value){
            if(value.includes('password')){
                throw new Error("error ");
            }
        }
    },
    tokens:[{
        token:{
        type:String,
        required:true
        }
    }]
})
// generating token
// this need to use function() not arrow function
userSchema.methods.generateAuthToken=async function(){
    const user=this;
    // console.log(user);
    const token=jwt.sign({_id:user._id.toString()},'helloworld');
    user.tokens=user.tokens.concat({token});
    await user.save();
    return token;
}
// login user
userSchema.statics.findByCredential=async(email,password)=>{
    const user= await User.findOne({email});
    if(!user){
        throw new Error("cannot find the given email");
    }
    isMatch=await bcrypt.compare(password,user.password);
    if(!isMatch){
        throw new Error("cannot find the user");
    }
    return user;
}

// hashing the password
userSchema.pre('save',async function(next){
    const user=this
    if(user.isModified('password')){
     
   user.password=await bcrypt.hash(user.password,8);
    }
    next();
})
const User=mongoose.model('User',userSchema);
module.exports=User

I know below line is used to save the token in database but can anyone can help to help understanding given line in easy way .I mean

  1. what is use of “user.tokens.concat ({token})”
  2. “user.tokens” statement can save data into DB?
  3. what is use of concat here?
user.tokens=user.tokens.concat({token});

router code

router.post('/users/login',async(req,res)=>{
    const user=await User.findByCredential(req.body.email,req.body.password);// findByCreditinal is made in user file
    const token= await user.generateAuthToken();
    if(!user){
        res.status(400).send();
    }
    else{
        res.status(201).send({user,token});
    }
})

response after sending post req

{
  "user": {
    "_id": "6298cd3386b6b15e303f0310",
    "name": "abc",
    "email": "abc@gmail.com",
    "age": 19,
    "password": "$2b$08$9Aq2GGX.BAVLUnNZpiEgJei0BJ9YKd9XTYPcwmpROyMJssg8Q.Cw.",
    "__v": 1,
    "tokens": [
      {
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2Mjk4Y2QzMzg2YjZiMTVlMzAzZjAzMTAiLCJpYXQiOjE2NTQ1Mjc2MzZ9.gpkKzhBriI_N3t8u9bkMgMbhqJghoUm3OAEUwuKbhus",
        "_id": "629e1694265e4491ecb4642c"
      }
    ]
  },
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2Mjk4Y2QzMzg2YjZiMTVlMzAzZjAzMTAiLCJpYXQiOjE2NTQ1Mjc2MzZ9.gpkKzhBriI_N3t8u9bkMgMbhqJghoUm3OAEUwuKbhus"
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.