0

I have a helper function where I create a new user.

The helper function looks like this

const createNewUser = (newUserDetails) => {
    return new Promise((resolve, reject) => {
      new User({
         fullName: newUserDetails.fullName,
         email: newUserDetails.email,
         image: newUserDetails.image,
         gender: newUserDetails.gender,
         age: newUserDetails.age
      }).save().then((response) => {
          resolve(response)
      }).catch((error) => {
          reject("Problem in Creating New User", error)
      })
    })
}

Now, For some reason, this code here looks very untidy to me

     fullName: newUserDetails.fullName,
     email: newUserDetails.email,
     image: newUserDetails.image,
     gender: newUserDetails.gender,

I was thinking if there is a way to destructure it? or if someone can help in shortening this code

Probably something like this

const createNewUser = (newUserDetails) => {
    return new Promise((resolve, reject) => {
      new User({newUserDetails
     }).save().then((response) => {
          resolve(response)
      }).catch((error) => {
          reject("Problem in Creating New User", error)
      })
    })
}

Not sure, if the above code will work or not, but I notice above I have reduced it to a single line

new User({newUserDetails

Can someone help me to restructure and reduce the length of the first function?

This is my mongoose schema

const mongoose = require('mongoose')

const userSchema = new mongoose.Schema({
    fullName: String,
    email: String,
    passowrd: String, 
    image: String, 
    age: Number, 
    gender: String,
    createdAt: {type: Date, default: Date.now}
}) 


module.exports = mongoose.model('User', userSchema);
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • What all properties does `newUserDetails` contain when passed to `createNewUser`? Does it contain only those 5 properties? – CertainPerformance Dec 24 '18 at 05:23
  • @CertainPerformance Yes – Alwaysblue Dec 24 '18 at 05:28
  • 1
    Just pass that object to `new User` then: `new User(newUserDetails)`, no need to extract the individual properties if they're already in the object. (and avoid the explicit Promise construction antipattern). (also, `passowrd: String, ` looks like a typo, might cause problems) – CertainPerformance Dec 24 '18 at 05:29
  • @CertainPerformance Can you please extend and elaborate on your statement `(and avoid the explicit Promise construction antipattern)` – Alwaysblue Dec 24 '18 at 05:31
  • see [first result on google](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it), just return the Promise chain – CertainPerformance Dec 24 '18 at 05:33
  • @CertainPerformance Can you please checkout this question and help me out https://stackoverflow.com/questions/53910226/understanding-explicit-promise-construction-anti-pattern – Alwaysblue Dec 24 '18 at 07:03

1 Answers1

1

Does this look better for you?

const createNewUser = (newUserDetails) => {
  return new Promise((resolve, reject) => {
    let {fullName, email, image, gender, age} = newUserDetails
    new User({ fullName, email, image, gender, age})
      .save().then((response) => {
        resolve(response)
      }).catch((error) => {
        reject("Problem in Creating New User", error)
      })
  })
}

You may alternatively use the object destructuring in the function parameter. I.e:

const createNewUser = ({fullName, email, image, gender, age}) => { ...
FLUSHER
  • 257
  • 2
  • 12