TypeError: Cannot set property 'email' of undefined










0















Am working on a project using Nodejs and am trying to apply the password encryption but still getting this error "TypeError: Cannot set property 'email' of undefined". please can anyone help me? THANKS



var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done)
done(null, user.id);
);

passport.deserializeUser(function(id, done)
User.findById(id, function(err,user)
done(err, user);
);
);

passport.use('local.signup', new LocalStrategy(
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
, function(req, email, password, done)
User.findOne('email': email, function(err, user)
if(err)
return done(err);

if(user)
return done(null, false, message: 'Email is already in use');

var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.encryptPassword(password);
newUser.save(function(err, result)
if(err)
return done(err);

return done(null, newUser);
);
);
));









share|improve this question
























  • newUser.local is not defined.

    – Teemu
    Nov 14 '18 at 6:20















0















Am working on a project using Nodejs and am trying to apply the password encryption but still getting this error "TypeError: Cannot set property 'email' of undefined". please can anyone help me? THANKS



var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done)
done(null, user.id);
);

passport.deserializeUser(function(id, done)
User.findById(id, function(err,user)
done(err, user);
);
);

passport.use('local.signup', new LocalStrategy(
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
, function(req, email, password, done)
User.findOne('email': email, function(err, user)
if(err)
return done(err);

if(user)
return done(null, false, message: 'Email is already in use');

var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.encryptPassword(password);
newUser.save(function(err, result)
if(err)
return done(err);

return done(null, newUser);
);
);
));









share|improve this question
























  • newUser.local is not defined.

    – Teemu
    Nov 14 '18 at 6:20













0












0








0








Am working on a project using Nodejs and am trying to apply the password encryption but still getting this error "TypeError: Cannot set property 'email' of undefined". please can anyone help me? THANKS



var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done)
done(null, user.id);
);

passport.deserializeUser(function(id, done)
User.findById(id, function(err,user)
done(err, user);
);
);

passport.use('local.signup', new LocalStrategy(
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
, function(req, email, password, done)
User.findOne('email': email, function(err, user)
if(err)
return done(err);

if(user)
return done(null, false, message: 'Email is already in use');

var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.encryptPassword(password);
newUser.save(function(err, result)
if(err)
return done(err);

return done(null, newUser);
);
);
));









share|improve this question
















Am working on a project using Nodejs and am trying to apply the password encryption but still getting this error "TypeError: Cannot set property 'email' of undefined". please can anyone help me? THANKS



var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done)
done(null, user.id);
);

passport.deserializeUser(function(id, done)
User.findById(id, function(err,user)
done(err, user);
);
);

passport.use('local.signup', new LocalStrategy(
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
, function(req, email, password, done)
User.findOne('email': email, function(err, user)
if(err)
return done(err);

if(user)
return done(null, false, message: 'Email is already in use');

var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.encryptPassword(password);
newUser.save(function(err, result)
if(err)
return done(err);

return done(null, newUser);
);
);
));






javascript node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 6:27







Chinedu Ernest

















asked Nov 14 '18 at 6:17









Chinedu ErnestChinedu Ernest

61




61












  • newUser.local is not defined.

    – Teemu
    Nov 14 '18 at 6:20

















  • newUser.local is not defined.

    – Teemu
    Nov 14 '18 at 6:20
















newUser.local is not defined.

– Teemu
Nov 14 '18 at 6:20





newUser.local is not defined.

– Teemu
Nov 14 '18 at 6:20












2 Answers
2






active

oldest

votes


















0














First, define newUser.local then newUser.local.email and password.



 var newUser = new User();
newUser.local = email:"", password:"";
newUser.local.email = email;
newUser.local.password = password;
newUser.save(function(err, result){
if(err)
return done(err);






share|improve this answer






























    0














    local is undefined. Try this:



    var newUser = new User();
    newUser.local =
    email : email,
    password : newUser.encryptPassword(password)






    share|improve this answer























    • It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

      – Chinedu Ernest
      Nov 14 '18 at 6:46











    • Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

      – iPirat
      Nov 14 '18 at 6:48











    • var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

      – Chinedu Ernest
      Nov 14 '18 at 6:51












    • The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

      – iPirat
      Nov 14 '18 at 7:21











    • Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

      – Chinedu Ernest
      Nov 14 '18 at 7:37










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294176%2ftypeerror-cannot-set-property-email-of-undefined%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    First, define newUser.local then newUser.local.email and password.



     var newUser = new User();
    newUser.local = email:"", password:"";
    newUser.local.email = email;
    newUser.local.password = password;
    newUser.save(function(err, result){
    if(err)
    return done(err);






    share|improve this answer



























      0














      First, define newUser.local then newUser.local.email and password.



       var newUser = new User();
      newUser.local = email:"", password:"";
      newUser.local.email = email;
      newUser.local.password = password;
      newUser.save(function(err, result){
      if(err)
      return done(err);






      share|improve this answer

























        0












        0








        0







        First, define newUser.local then newUser.local.email and password.



         var newUser = new User();
        newUser.local = email:"", password:"";
        newUser.local.email = email;
        newUser.local.password = password;
        newUser.save(function(err, result){
        if(err)
        return done(err);






        share|improve this answer













        First, define newUser.local then newUser.local.email and password.



         var newUser = new User();
        newUser.local = email:"", password:"";
        newUser.local.email = email;
        newUser.local.password = password;
        newUser.save(function(err, result){
        if(err)
        return done(err);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 6:22









        amiramir

        9031026




        9031026























            0














            local is undefined. Try this:



            var newUser = new User();
            newUser.local =
            email : email,
            password : newUser.encryptPassword(password)






            share|improve this answer























            • It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

              – Chinedu Ernest
              Nov 14 '18 at 6:46











            • Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

              – iPirat
              Nov 14 '18 at 6:48











            • var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

              – Chinedu Ernest
              Nov 14 '18 at 6:51












            • The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

              – iPirat
              Nov 14 '18 at 7:21











            • Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

              – Chinedu Ernest
              Nov 14 '18 at 7:37















            0














            local is undefined. Try this:



            var newUser = new User();
            newUser.local =
            email : email,
            password : newUser.encryptPassword(password)






            share|improve this answer























            • It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

              – Chinedu Ernest
              Nov 14 '18 at 6:46











            • Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

              – iPirat
              Nov 14 '18 at 6:48











            • var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

              – Chinedu Ernest
              Nov 14 '18 at 6:51












            • The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

              – iPirat
              Nov 14 '18 at 7:21











            • Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

              – Chinedu Ernest
              Nov 14 '18 at 7:37













            0












            0








            0







            local is undefined. Try this:



            var newUser = new User();
            newUser.local =
            email : email,
            password : newUser.encryptPassword(password)






            share|improve this answer













            local is undefined. Try this:



            var newUser = new User();
            newUser.local =
            email : email,
            password : newUser.encryptPassword(password)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 6:29









            iPiratiPirat

            1,492818




            1,492818












            • It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

              – Chinedu Ernest
              Nov 14 '18 at 6:46











            • Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

              – iPirat
              Nov 14 '18 at 6:48











            • var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

              – Chinedu Ernest
              Nov 14 '18 at 6:51












            • The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

              – iPirat
              Nov 14 '18 at 7:21











            • Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

              – Chinedu Ernest
              Nov 14 '18 at 7:37

















            • It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

              – Chinedu Ernest
              Nov 14 '18 at 6:46











            • Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

              – iPirat
              Nov 14 '18 at 6:48











            • var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

              – Chinedu Ernest
              Nov 14 '18 at 6:51












            • The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

              – iPirat
              Nov 14 '18 at 7:21











            • Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

              – Chinedu Ernest
              Nov 14 '18 at 7:37
















            It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

            – Chinedu Ernest
            Nov 14 '18 at 6:46





            It still didn't work, it now says: 'TypeError: newUser.encryptPassword is not a function'

            – Chinedu Ernest
            Nov 14 '18 at 6:46













            Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

            – iPirat
            Nov 14 '18 at 6:48





            Then encryptPassword is not a function. Can’t say anything about that without seeing the User class

            – iPirat
            Nov 14 '18 at 6:48













            var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

            – Chinedu Ernest
            Nov 14 '18 at 6:51






            var mongoose = require('mongoose'); var Schema = mongoose.Schema; var bcrypt = require('bcrypt-nodejs'); var userSchema = new Schema( email:type:String, required:true, password:type:String, required: true ); userSchema.methods.encryptPassowrd = function(password) return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null); ; userSchema.methods.validPassword = function(password) return bcrypt.compareSync(password, this.password); ; module.exports = mongoose.model('User',userSchema);

            – Chinedu Ernest
            Nov 14 '18 at 6:51














            The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

            – iPirat
            Nov 14 '18 at 7:21





            The method in Schema is called encryptPassowrd you call encryptPassword which doesn’t exist

            – iPirat
            Nov 14 '18 at 7:21













            Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

            – Chinedu Ernest
            Nov 14 '18 at 7:37





            Yes! Yes! Yes!!! Thank you very much @iPirat, it works now. the error was i used 'encryptPassowrd' instead of 'encryptPassword' like you rightly said. Thanks Again.

            – Chinedu Ernest
            Nov 14 '18 at 7:37

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294176%2ftypeerror-cannot-set-property-email-of-undefined%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto