Exercise Tracker Project: Object id returning "undefined"

I’m creating the exercise tracker project for the API’s and Microservices path. I create a user with a specific username and “_id”. Which I know is working because I can see the addition to mongodb. However, when I then try to use the “_id” in the function that will add exercises, the “_id” always comes back undefined. I was wondering if anyone could help me out.

here is the code in my server.js, the issues happens in the “app.post(”/api/users/:_id/exercises" part

//All requirements
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const multer = require("multer");
const upload = multer();
const cors = require("cors");
const mongoose = require("mongoose");

const Schema = mongoose.Schema
//Connect to DB
mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  // useUnifiedTopology: true,
  useCreateIndex: true

//Use the requirements
app.use(bodyParser.urlencoded({ extended: true }));

// // let's make json a bit cleaner

//Send the HTML
app.get("/", (req, res) => {
  res.sendFile(__dirname + "/views/index.html");

const personSchema = new Schema({ username: String})
const user = mongoose.model('user', personSchema)

const exerciseSchema = new Schema(
      description: String,
      duration: Number,
      date: Date
const exercise = mongoose.model('exercise', exerciseSchema)

app.post("/api/users", (req,res)=>{
  const newPerson = new user({username:req.body.username});
    res.json({username: data.username, "_id": data._id})

app.post("/api/users/:_id/exercises", (req,res) =>{

  const {userId, description, duration, date} = req.body;

  user.find({"_id":userId}, (err,data) =>{
      res.send("Unknown userId")
      const newExercise = new exercise({userId, description, duration, date});
      const username = data.username;
        res.json({userId, username, description, duration, date})

// Not found middleware
app.use((req, res, next) => {
  return next({ status: 404, message: "not found" });

// Error Handling middleware
app.use((err, req, res, next) => {
  let errCode, errMessage;

  if (err.errors) {
    // mongoose validation error
    errCode = 400; // bad request
    const keys = Object.keys(err.errors);
    // report the first validation error
    errMessage = err.errors[keys[0]].message;
  } else {
    // generic or custom error
    errCode = err.status || 500;
    errMessage = err.message || "Internal Server Error";

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36

Challenge: Exercise Tracker

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

NeverMind I figured it out

Congratulations on solving the problem!