APIs projects: Exercise tracker project

Hi! So I’m working on the exercise tracker project and I get an error when starting the server. I tried to find the cause of the error in google but couldn’t find something helpful.The error occurs on the provided part of server.js file and precisely in then() after the console.log("connected");

Error message:

Something went wrong ObjectParameterError: Parameter "obj" to Document() must be an object, got function(req, res, next) {
    app.handle(req, res, next);
  }
at...
.
.
.
at path/to/server.js/server.js
at processTicksAndRejections (node:internal/process/task_queues:96:5)

Here are 2 pieces of code (User model and the part of server.js where getting the error)
user.js:

var mongoose = require('mongoose');
const { Schema } = mongoose;

// Define user schema
const userSchema = new Schema({
    username: {type: String, required: true},
    exercises: [Object]
});

module.exports = mongoose.model('User', userSchema);

server.js:

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true}).then(() => {
    console.log('connected');
    user(app)
    exercise(app)
    routes(app)
  })
  .catch((e) => {
    console.log("Something went wrong", e);
  });

Do you have a link to a Replit?

Are you sure Object is a valid type? If you want an array and the element type is a schema type it is just the name of the schema.

Unfortunatelly, I’m working on localhost. For Object, It was just a try to check if it is accepted like Number, etc. I will change it to Exercise which is really what I want as type and see if the problem persist. Thanks for your help.

Solved by:

  • replacing exercises: [Object] with exercises: {type: [mongoose.Schema.Types.ObjectId], ref:'exercise'}

  • removing: user(app) and exercise(app)

Current code:
server.js:

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true}).then(() => {
    console.log('connected');
    routes(app)
  })
  .catch((e) => {
    console.log("Something went wrong", e);
  });

user.js:

var mongoose = require('mongoose');

const { Schema } = mongoose;

// Define user schema
const userSchema = new Schema({
    username: {type: String, required: true},
    exercises: {type: [mongoose.Schema.Types.ObjectId], ref:'exercise'}
});

module.exports = mongoose.model('User', userSchema);

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