Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
'TypeError: Cannot set property 'create' of undefined'?

I'm trying to create a prototype method for a "User" object. How is "create" undefined when I am literally declaring it to a function?

const mongoose = require("mongoose")

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: [true, "Field cannot be left blank"],
    unique: [true, "That username is already taken"],
    minlength: [3, "Username must be at least 3 characters"],
    maxlength: [15, "Username cannot exceed 15 characters"],
  },
})

const Doc = new mongoose.model("user", userSchema)

let User = (username) => {
  this.username = username
}

User.prototype.create = function () {
  return new Promise(async (resolve, reject) => {
    try {
      let doc = await new Doc({
        username: this.username,
      })
      resolve(doc)
    } catch (err) {
      reject(err)
    }
  })
}

module.exports = User
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I was able to solve it by changing the arrow function of my constructor to an anonymous function. I did some research and ES6 does not allow constructor functions to be arrow functions.

//previous code

let User = (username) => {
  this.username = username
}

//correct code

let User = function(username) {
  this.username = username
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!