node.js - How to allow users to update their profile? -


hey trying allow users edit profiles. however, fields not exist in document, unless add field. here how calling query error if field not exist.

here code routes file:

user.findbyidandupdate(req.signedcookies.userid,{                 firstname: req.body.firstname.tolowercase(),                 lastname: req.body.lastname.tolowercase(),                 email: req.body.email.tolowercase(),                 firstnametrue: req.body.firstname,                 lastnametrue: req.body.lastname,                 emailtrue: req.body.email,                 emaillist: req.body.newemail,                  phone: req.body.phone,                 phonelist: req.body.newphone,                 socialaccounts: {socialaccount: req.body.socialaccount, socialaddress: req.body.socialaccountnew},                 currentcity: req.body.currentcity,                 birthday: new date(req.body.birthday)              }, function(err, user) {                 console.log('here1');                 if(err) {                     console.log("post2");                     console.log(err);                     res.render('edituserprofileerror', {title: 'weblio'});                  } else {             console.log("post3");                     res.redirect('userprofile');                 }             });  };   

the error getting :

[typeerror: cannot read property 'constructor' of undefined] 

i using nodejs mongoose mongodb. have every field in mongoose schema, not saved in db unless added manually users.

what have tried seems big pain, loop through fields value , see ones exist, throw them in array query array

here 1 solution tried not working correctly since need allow ':' pass through...

 var values = [                 firstnameval =  req.body.firstname.tolowercase(),                 lastnameval =  req.body.lastname.tolowercase(),                 emailval =  req.body.email.tolowercase(),                 firstnametrueval =  req.body.firstname,                 lastnametrueval =  req.body.lastname,                 emailtrueval =  req.body.email,                 emaillistval =  req.body.newemail,                  phoneval =  req.body.phone,                 phonelistval =  req.body.newphone,                 currentcityval =  req.body.currentcity,                 birthdayval =  new date(req.body.birthday)         ];          var  keyicons = [             firstname,             lastname,             email,             firstnametrue,             lastnametrue,             emailtrue,             emaillist,              phone,             phonelist,             currentcity,             birthday         ];          var existvalues =[];         for(var x = 0; x <keyicons.length; x++) {             for(var = 0; < values.length; i++) {                 if(values[i] === undefined) {                     (console.log('undefined'))                 } else {                     existvalues.push({keyicons[i] : values[i]});                 }             };         }          var socialaccountsval =  {socialaccount: req.body.socialaccount, socialaddress: req.body.socialaccountnew}         if(socialaccountsval.socialaccount === undefined) {          } else {             existvalues.push(socialaccounts);         }; 

another solution might able query user doc , see values available confused how go it...

also, feel lie there should easier way it?

edit

here schema:

var mongoose = require('mongoose'),     schema = mongoose.schema,     objectid = mongoose.schema.types.objectid,     bcrypt = require('bcrypt-nodejs'),     salt_work_factor = 10;    var userschema = new schema({      email: { type: string, required: true, lowercase:true, index: { unique: true } }, //might have take off lowercase     emailtrue: { type: string},     emailprivate: {type: boolean},     emaillist: {type: array},     password: { type: string, required: true },     firstname: {type: string, lowercase:true, required: true, index: true},     firstnametrue: { type: string},     lastname: {type: string, lowercase:true, required: true, index: true},     lastnametrue: { type: string},     phone: {type: number, required: true},     phoneprivate: {type: boolean},     phonelist: {type: array},     birthday: {type: date, required: true},     birthdayprivate: {type: boolean},     socialaccounts: {type: array},     currentcity: {type: string},     date_created: {type: date},     email_confirmed: {type: boolean},     gender: {type: number},     currentdevice: {type: string},     last_login: {type: date} }, {collection: "users"});  module.exports = mongoose.model('user', userschema); 

you first construct object fields in req, , pass object mongoose method:

var userfields = {}; if (req.body.firstname) userfields.firstname = req.body.firstname.tolowercase(); if (req.body.lastname) userfields.lastname = req.body.lastname.tolowercase(); ...etc...  user.findbyandupdate(req.signedcookies.userid, userfields, function(err, user) {     ... }); 

this allows sanitization of fields in request before passing them on database.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -