# 9.2 Referencing Documents. The course code throws a validation error

**URL:** <https://forum.codewithmosh.com/t/9-2-referencing-documents-the-course-code-throws-a-validation-error/17511>\
**Category:** Node\
**Created:** [January 7, 2023, 4:48am UTC](https://forum.codewithmosh.com/t/9-2-referencing-documents-the-course-code-throws-a-validation-error/17511 "2023-01-07T04:48:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![notaniceusername](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@notaniceusername](https://forum.codewithmosh.com/u/notaniceusername)\
**Post date:** [January 7, 2023, 4:48am UTC](https://forum.codewithmosh.com/t/9-2-referencing-documents-the-course-code-throws-a-validation-error/17511/1 "2023-01-07T04:48:18Z")

</div>

When I follow the course code

```auto
const Course = mongoose.model('Course', new mongoose.Schema({
  name: String,
  author : {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Author'
  }
}));

```

I got this error

```auto
ValidationError: Course validation failed: author: Cast to ObjectId failed for value "authorId" (type string) at path "author" because of "BSONTypeError"

```

My mongoose version is 6.8.3. Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![maverick](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@maverick](https://forum.codewithmosh.com/u/maverick)\
**Post date:** [January 7, 2023, 7:07am UTC](https://forum.codewithmosh.com/t/9-2-referencing-documents-the-course-code-throws-a-validation-error/17511/2 "2023-01-07T07:07:19Z")

</div>

That is a validation issue:  
in your validation logic use something like:

```auto
// you have a mongoose object already.
// i.e., const mongoose = require('mongoose');

if(!mongoose.Types.ObjectId.isValid(authorId)){
    // set the validation message here.
    // Note: this logic is by default available in mongoose. But you can use some Joi extension 
    // that can do this for you.
}

```

Problem: you probably don’t have a valid authorId and you are passing string **OR** you have a valid author id but you are not using a proper validator to check it’s type. Meaning, by default your API call POST data will be treated as string values and ObjectId is not a string ([ObjectId](https://www.mongodb.com/docs/manual/reference/method/ObjectId/)).

Let us know if it helped or not.

If this doesn’t help, please show us more code.

---

<div class="post-metadata">

**Author:** ![notaniceusername](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@notaniceusername](https://forum.codewithmosh.com/u/notaniceusername)\
**Post date:** [January 7, 2023, 8:34pm UTC](https://forum.codewithmosh.com/t/9-2-referencing-documents-the-course-code-throws-a-validation-error/17511/3 "2023-01-07T20:34:02Z")

</div>

Thank you for your reply. I found that I forgot to change the ‘authorID’ to the actual ID.

---

<div class="post-metadata">

**Author:** ![baloer12](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@baloer12](https://forum.codewithmosh.com/u/baloer12)\
**Post date:** [January 23, 2023, 7:55am UTC](https://forum.codewithmosh.com/t/9-2-referencing-documents-the-course-code-throws-a-validation-error/17511/4 "2023-01-23T07:55:02Z")

</div>

hallo sir. I beginner with mongodb and we have same problem, can you show solve code of you sir?
