From mongoose to swagger: "<field>" validation error - BSONTypeError: The argument provided must be a string containing either 12 bytes or 24 hexadecimal characters

I'm currently working on developing a REST API using Express, Mongoose, and Swagger for API documentation. To automate the process of converting my existing schema to Swagger, I utilized the mongoose-to-swagger package. However, I encountered an issue where Swagger was adding an extra "_id" field in the schema as depicted in the image.

https://i.sstatic.net/MqUQl.png

Moreover, when attempting to post a new user, I received the following error message:

ValidationError: Buyer validation failed: Buyer_Delivery_Address._id: Cast to ObjectId failed for value "1" (type number) at path "_id" due to "BSONTypeError"

reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer

_message: 'Buyer validation failed'

How can this issue be resolved?

The App.js

`


const express = require("express");
const mongoose = require("mongoose");
const m2s = require('mongoose-to-swagger');
const swaggerjsdocs = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const bodyParser = require("body-parser")
const Buyer = require('./models/Buyer_model')

// Code continues...

`

Buyer_model.js

`

const mongoose = require("mongoose")

const buyerSchema = new mongoose.Schema({
    // Schema definition
})

module.exports = mongoose.model('Buyer', buyerSchema)

Answer №1

As stated here, it is possible to include an extra parameter with the "omit" property that requires an array of fields to exclude. While I have not tested this in a nested model, it should function properly after implementing the "omit" property in all nested models as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Bootstrap Modal consistently shows the contents of the first record when used within a SQL While Statement

As a newcomer, I am in the process of converting an operational Bootstrap modal that houses a post form. Initially, the modal would open using a button with a specific Id and would always display the FORM contents from the first record found in the SQL w ...

Playing with vue component dependencies

During my attempt to run a unit test, I encountered an error when making an axios call. To handle this error, I successfully mocked the call but faced an issue when trying to utilize an external library dependency (specifically, vue-toasted) to display an ...

Tips for transferring the content of a variable to a handlebars block

Currently, I am grappling with HTML code that involves handlebars templates to store internal variables. While I may not be an expert in handlebars, I am trying my best to navigate through it. The crux of my issue lies in the need to access a lengthy list ...

After logging in, I am unable to redirect to another PHP page as the login form simply reloads on the same page

Lately, I've encountered an issue that has persisted for a few days. The login validation is functioning flawlessly. However, the problem arises when attempting to redirect to another page, specifically 'index.php', after logging in. Instead ...

Node/ExpressJs Migration Module: A Comparison to Rails ActiveRecord Migrations

Having come from the Ruby world and now learning Express.js, I was wondering if there is a Node/ExpressJs library similar to Rails ActiveRecord Migrations? ...

Fulfill the specified amounts for each row within a collection of items

I have an array of objects containing quantities. Each object includes a key indicating the amount to fill (amountToFill) and another key representing the already filled amount (amountFilled). The goal is to specify a quantity (amount: number = 50;) and au ...

When making a post request, req.body may be null

I am attempting to log the body content of a request to the console within a post function. Here is the code snippet: export const createOffering = async (req, res) => { const offering = req.body; const newOffering = new Offering(offering); console.log( ...

Is there a way to pass a method along with its specific generic type to RXJS bindCallback?

When trying to pass a function with callback to the rxjs bindCallback function, I encountered an issue with my generic type. Here is the code snippet in question: const obCreator = bindCallback<T>(FakeServer.instance.on<T>); return obCreator(m ...

Step-by-step Guide on Utilizing Bootstrap Table

Currently, I am in the process of fetching data from an API and displaying it in a tabular format using JavaScript. While I have successfully retrieved the data, I'm facing an issue with applying Bootstrap styling to the table headers. async functi ...

Explore the intricacies of complex hierarchies

<table id="financial101_tab1" class="dxrpControl_Moderno dxrpWithoutHeader_Moderno"> <tbody> <tr> <td id="financial101_tab1_RPC" class="dxrp dxrpcontent"> <input id="BlockControlfinancial101_tab1ATI" type= ...

Analyze two sets of JSON data and compile a new JSON containing only the shared values

I am trying to generate two JSON arrays based on a shared property value from comparing two sets of JSON data. this.linkedParticipants =[ { "id": 3, "name": "Participant 2", "email": "<a href="/ ...

What is the best way to pass a value back to the main function from an async.eachOfSeries function?

Currently, I am utilizing the async npm library in my project. I am interested in finding a way to return the value of 'someVar' back to the main function. The documentation indicates that it returns a promise if a callback is not provided. Howe ...

Automatically switch to dark mode at specified times: A simple guide

Here is the current method for toggling themes: let themeToggler = document.getElementById('theme-toggler'); themeToggler.onclick = () => { themeToggler.classList.toggle('fa-sun'); if (themeToggler.classList.contains('f ...

React and React Native not synchronizing with authentication context

It seems like there is an issue with the AuthContext not updating properly. Despite logging at various points, the user is still not being set. Here's a glimpse of the code in question: App.tsx export default function App() { const { user, setUser ...

The context provider in Next.js wraps the App component with a layout component specific to each page, resulting in data

Within my project, I have an authentication context component wrapping the main app component. Simultaneously, I am also attempting to implement a page-specific layout component based on the Next.js documentation found here. I'm unsure if my approach ...

How can I make Material UI's grid spacing function properly in React?

I've been utilizing Material UI's Grid for my layout design. While the columns and rows are functioning properly, I've encountered an issue with the spacing attribute not working as expected. To import Grid, I have used the following code: ...

When Sequelize is utilized with the include option, it is encountering issues by returning null values

When performing a model query with includes and there are no matching rows, the result returned includes null values. I have come across this issue multiple times before, but I have yet to find a definitive solution. Model.findOne({ where: { id: 1 }, ...

The functionality of a button within an AngularJS directive is not functioning as intended

I am trying to use a directive twice on one page. Inside the directive, there is a button that should toggle between showing the two directives when clicked. However, I'm encountering an issue where the values are not changing even though the ng-click ...

Leveraging generics within TypeScript

I have developed a class in TypeScript that uses generics. export class ModelTransformer { static hostelTransformer: HostelTransformer; static fromAtoB(instance: T): U { if (instance instanceof HostelType) { return ModelTrans ...

The crossIcon on the MUI alert form won't let me close it

I am facing an issue with my snackBar and alert components from MUI. I am trying to close the alert using a function or by clicking on the crossIcon, but it's not working as expected. I have used code examples from MUI, but still can't figure out ...