user9817924
user9817924

Reputation:

Nodejs saving to mongoDB

I want to save data to database but i dont know how to do it in this case.

userModel.js

const userSchema = new mongoose.Schema({
  email: {
    type: String,
    unique: true
  },
  password: String,
  passwordResetToken: String,
  passwordResetExpires: Date,

  profile: {
    firstname: String,
    lastname: String,
    gender: String,
    status: String,
    location: String,
    website: String,
    picture: String
  }
});

Signup.js

 const user = new User({
    firstname: req.body.firstname,
    lastname: req.body.lastname,
    location: req.body.location,
    status: 0,
    email: req.body.email,
    password: req.body.password
  });

Problem is that those things in profile wont save and i dont know how to edit that signup.js to make it work. Something like profile.firstname: req.body.firstname does not work.

Upvotes: 0

Views: 51

Answers (2)

Amol B Jamkar
Amol B Jamkar

Reputation: 1257

You need to save like this in signUp.js

const user = new User({
    profile :{
        firstname: req.body.firstname,
        lastname: req.body.lastname,
        location: req.body.location,
        status: 0
    },
    email: req.body.email,
    password: req.body.password
  });

As firstname, lastname, location and status are part of the profile object.

Upvotes: 0

Wang Liang
Wang Liang

Reputation: 4434

const user = new User({
    profile: {
        firstname: req.body.firstname,
        lastname: req.body.lastname,
        location: req.body.location,
        status: 0,
    },
    email: req.body.email,
    password: req.body.password
});

From schema (userModel.js), we need update with profile structure

Upvotes: 1

Related Questions