# Chat GPT Helped debug my code and taught me!

**URL:** <https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903>\
**Category:** I Built This\
**Created:** [January 26, 2023, 11:14pm UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903 "2023-01-26T23:14:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdventureBear](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/adventurebear/32/74786_2.png) [@AdventureBear](https://forum.freecodecamp.org/u/AdventureBear)\
**Post date:** [January 26, 2023, 11:14pm UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903/1 "2023-01-26T23:14:02Z")

</div>

I’ve been working on the Exercise Tracker for 2 days now and have posted 4 questions after finding similar, but not identical, but have received zero responses to my queries.

Two I solved on my own but still had errors on testing despite it working for me fine. Now I’m sure a lot of the experienced coders would spot some errors right away, but the “Cast Error” was driving me crazy.

Here is the conversation:  
me:  
_can you help me with my node.js and express project?_

Chat:  
_Of course! What do you need help with specifically in your Node.js and Express project?_

me:  
_i need help understanding why this piece of code gives me a “cast error”_

```auto

// save new exercise from form
app.route('/api/users/:_id/exercises')
  .post((req, res) => { //receive form data
    console.log(req.body);
    console.log(req.params);
    console.log(req.query);
    let exerciseDate = new Date()

    let userID = req.params._id
    console.log(userID)

    User.findById({ _id: userID }, function(err, user) {
      console.log(user)
      if (err) return console.log(err)
      if (req.body.date != '') {
        let exerciseDate = req.body.date
      }

      var exercise = new Exercise({
        username: user.username,
        descpription: req.body.description,
        duration: req.body.duration,
        date: exerciseDate
      })

      exercise.save(function(err, data) {
        if (err) return console.err(err);
        done(null, exercise);
      })
      console.log(exercise)
      res.json(exercise)

    })

  }
  )

```

Chatbot:

_The “cast error” is likely caused by the fact that the `userID` being passed as a parameter in the `findById` method is not a valid MongoDB ObjectId. The `findById` method is used to find a single document by its `_id` field, which must be a valid ObjectId._

_It seems that you are trying to get user id from req.params.\_id, and use it to find the user in your database. But, if the user id is passed as a string in the request, you need to convert it to a ObjectId before passing it to the `findById` method. You can do this by using the `mongoose.Types.ObjectId` function, like this:_

```auto
const mongoose = require('mongoose');
let userID = mongoose.Types.ObjectId(req.params._id);

```

_Also, it seems that you are using a callback function to find the user, but you are not handling the error properly. You should check if the error is null, if not, return the error, like this:_

```auto
User.findById({ _id: userID }, function(err, user) {
    if (err) return res.status(400).json({error : "user not found"});
    // your code
}

```

_Also, in the code you shared, `console.err(err);` should be `console.error(err);`. Additionally, `done(null, exercise);` should not be there, as `done` is not defined in the scope._

_Let me know if this helps or if you have any other questions._

Holy smokes…it worked and I am floored!!

---

<div class="post-metadata">

**Author:** ![AdventureBear](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/adventurebear/32/74786_2.png) [@AdventureBear](https://forum.freecodecamp.org/u/AdventureBear)\
**Post date:** [February 2, 2023, 4:46am UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903/5 "2023-02-02T04:46:07Z")

</div>

Hey thanks for your feedback. I did end up changing the code all around anyway, but I made this change suggested and it worked, but didn’t work for some later challenges. I forget all the details now but you’re right…wonder if I should just remove this thread so as not to confuse anyone?

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [February 2, 2023, 9:46am UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903/6 "2023-02-02T09:46:45Z")

</div>

We need to see all the code. I’m guessing the cast error might have to do with how you defined the id in the schema (which you usually do not even need to do).

* * *

I haven’t personally tried ChatGPT yet but I do think you have to be careful with how you use it. If you can not verify the answer because you do not have the knowledge to do so it can pretty much tell you anything. In the end, the onus is on you to verify it.

I do think it might be interesting to see it set up for pair programming, and see how it handles the driving and navigation.

---

<div class="post-metadata">

**Author:** ![AdventureBear](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/adventurebear/32/74786_2.png) [@AdventureBear](https://forum.freecodecamp.org/u/AdventureBear)\
**Post date:** [February 2, 2023, 10:54pm UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903/7 "2023-02-02T22:54:05Z")

</div>

I agree. The answers it gave me seemed to be something right out of the mongo / express / mongoose / node docs but collated to give me a cohesive answer without me having to look up 4 different sets of docs.

I eventually passed the challenge but if you’re willing I could share my code to see what bad practices I am guilty of so I can improve

---

<div class="post-metadata">

**Author:** ![ringoj66](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ringoj66/32/30098_2.png) [@ringoj66](https://forum.freecodecamp.org/u/ringoj66)\
**Post date:** [February 21, 2023, 2:23am UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903/8 "2023-02-21T02:23:40Z")

</div>

so chatGPT is not better than human programmers afterall?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 22, 2023, 2:23pm UTC](https://forum.freecodecamp.org/t/chat-gpt-helped-debug-my-code-and-taught-me/587903/9 "2023-08-22T14:23:49Z")

</div>

Closed on August 22, 2023, 2:23pm UTC

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