'Error: Cast to undefined failed for value undefined at path "undefined"
I am a beginner JavaScript developer. I am getting this error when I try to get all soldiers by route: "localhost:3005/api/v1/soldiers" in POSTMAN.
In order to work on NodeJS with ES module imports, I did the following:
npm install --save-dev @babel/cli @babel/core @babel/node @babel/plugin-proposal-class-properties @babel/preset-env babel-loader
After that, a .balerc file was created at the root of the folder
server/.babelrc
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-class-properties"],
"ignore": [
]
}
The next step was to create the nodemon.json file
server/nodemon.json
{
"watch": [
"src",
".env"
],
"ext": "js,json",
"ignore": [
"src/**/*.spec.js",
"src/**/*.test.js"
],
"exec": "babel-node ./src/index.js"
}
After that I wrote Schema for HomePost, services, controllers and routes and used --> import mongoose from 'mongoose'. When sending any requests, everything worked fine. Find by ID, delete by ID, create home post, get home posts list. I started writing a similar schema, services, controllers and routes for soldiers.
models/sordier.model.js
import mongoose from 'mongoose'
const Soldier = new mongoose.Schema({
title: {
type: String,
required: true
},
image: {
type: String,
required: true
},
rank: {
type: String,
required: true
},
status: {
type: String,
default: '1'
},
created_at: {
type: Date,
default: new Date()
},
updated_at: {
type: Date,
default: new Date()
}
})
export default mongoose.model('Soldier', Soldier)
services/soldiers.service.js
export const getSoldiers = async ({
offset = 0,
limit = 9
}) => Soldier.find({}, null, {
skip: parseInt(offset),
limit: parseInt(limit)
})
api/soldiers/soldiers.controller.js
export const SoldiersListController = async (request, response, next) => {
try {
const { offset, limit } = request.query
const soldiers = await getSoldiers({
offset,
limit
})
return response
.status(200)
.json({
status: true,
soldiers: Array.isArray(soldiers)
? soldiers?.map(soldier => SoldiersListItemMapper(soldier))
: []
})
} catch (error) {
console.log(error);
next(error)
}
return response
.status(200)
.json({
success: true,
data
})
}
Using POSTMAN, I made sure that the function works: create a soldier, get one by ID, delete one by ID, but getting a list of soldiers does not work.
I am catching the error:
Error: Cast to undefined failed for value undefined at path "undefined"
Then in DEBUG CONSOLE I find:
/usr/local/bin/node ./server/src/models/soldier.model.js
Process exited with code 1
(node:72959) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
Adding --> "type": "modules" to my package.json breaks all other imports in the application. I want to continue writing with ES module imports and not rewrite to --> const variable = require('path') <--
What can I do?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
