Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CastError: Cast to [undefined] failed for value "["user"]" at path "roles" #8058

Closed
AdamBCo opened this issue Aug 10, 2019 · 5 comments
Closed
Milestone

Comments

@AdamBCo
Copy link

AdamBCo commented Aug 10, 2019

I am currently receiving the following error when running a Mongoose query in a test environment utilizing supertest and jest.

CastError: Cast to [undefined] failed for value "["user"]" at path "roles"

I am using Mongoose 5.6.9 with the code below:

The Query

const _id = req.params._id

User.findOne({
	_id,
	roles: 'admin'
})
	.exec()
	.then(user => {
		if (!user) {
			return next(boom.badRequest("No User"))
		}
		res.status(200).json(user)
	})
	.catch(err => next(boom.badRequest(err)))

The User Model

const mongoose = require('mongoose')

const Roles = ['user', 'admin', 'gallery', 'artist', 'appraiser', 'beta', 'super', 'contractor', 'auction_house']

const schema = mongoose.Schema(
    {
		name: {
			type: String,
			trim: true
		},
		roles: {
	        type: [{
	            type: String,
	            enum: Roles
	        }],
	        default: ['user']
	    }

    },
	{
		timestamps: {
			createdAt: 'created_at',
			updatedAt: 'updated_at'
		}
	}
)

module.exports = mongoose.model('User', schema)

Can someone please help me figure out what's going on?

Thank you

@esetnik
Copy link

esetnik commented Aug 10, 2019

Maybe this is related to #8053

@AdamBCo
Copy link
Author

AdamBCo commented Aug 10, 2019

@esetnik Thanks for pointing me to that issue. Unfortunately it showcases the creation of a User object; whereas I am simply trying to find one. I'm not sure where the case issue is occurring.

@vkarpov15 vkarpov15 added this to the 5.6.10 milestone Aug 11, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 11, 2019
@vkarpov15
Copy link
Collaborator

The below script executes without error for me on 5.6.9, please modify the below script to demonstrate your issue.

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh8058`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString, { useNewUrlParser: true });
  await mongoose.connection.dropDatabase();
  
  const schema = mongoose.Schema(
    { 
      name: { 
        type: String,
        trim: true
      },
      roles: {
        type: [{
          type: String,
          enum: ['admin', 'super-admin']
        }],
        default: ['user']
      }
    });

  const User = mongoose.model('User', schema);

  await User.create({ name: 'test', roles: ['admin'] });
  const doc = await User.findOne({ roles: 'admin' });
  console.log(doc);
}

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 11, 2019
@vkarpov15 vkarpov15 removed this from the 5.6.10 milestone Aug 11, 2019
@vkarpov15
Copy link
Collaborator

This looks to be a duplicate of #8053. It's a bug in Jest that we'll work around.

I strongly recommend moving off of Jest. Jest is an anti-pattern because it does so much monkeypatching that it creates a distinct JavaScript environment that isn't quite Node.js or a browser. Code that works in Jest may not work in Node.js and vice versa.

@vkarpov15 vkarpov15 added this to the 5.6.10 milestone Aug 18, 2019
@vkarpov15 vkarpov15 removed the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Aug 18, 2019
@realeibo
Copy link

I had the same problem - I updated to 5.6.11 and it fixes it.

@Automattic Automattic locked as resolved and limited conversation to collaborators Sep 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants