Exercise-Tracker: Can't get past first few checks

Hello, I am on the exercise-tracker of Microservices and API’s subject. I Cannot get past the first few checks of the assignment. Specifically the " You can POST to /api/users with form data username to create a new user." and the two after it. The error message is: “ReferenceError: createPerson is not defined”
How can I get it to work and pass the tests? What did I miss?

Here is my code:

const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require("mongoose")
require('dotenv').config()
app.use(cors())
app.use(express.static('public'))

const Schema = mongoose.Schema

const personSchema = new Schema({
  username: { type: String, required: true},
})

const exerciseSchema = new Schema({
  description: { type: String, required: true},
  duration: { type: Number, required: true},
  date: { type: Date, required: true},
})

const Person = mongoose.model("person", personSchema)

const Exercises = mongoose.model("exercises", exerciseSchema)


app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
});


app.post("/api/users", (res) => {
  res.json({guy})
  const createPerson = (finish) =>{
    let guy = new Person({
      username: "hey hey 1",
    })
    guy.save((error, data) =>{
      if(error){
        console.log(error)
      }
      else{
        finish(null, data)
      }
    })
  }

})




const listener = app.listen(process.env.PORT || 3000, () => {
  console.log('Your app is listening on port ' + listener.address().port)
})

exports.PersonModel = Person;
exports.createPerson =  createPerson;

(FYI: If you’re wondering why am not using mongodb or calling it to connect in my code it is because I am using the VSC extension for MongoDB which does that for me automatically).

How are you using the last 2 lines of your code?

createPerson is scoped to the route handler. It should be declared in an outer scope (for the exports).

to export the models to the database

Do you mean like this?

 const createPerson = (finish) =>{
    let guy = new Person({
      username: "hey hey 1",
    })
    guy.save((error, data) =>{
      if(error){
        console.log(error)
      }
      else{
        finish(null, data)
      }
    })
  }

app.post("/api/users", (res) => {
  res.json({createPerson})

})

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.