-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
Maybe this is related to #8053 |
@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. |
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);
} |
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. |
I had the same problem - I updated to 5.6.11 and it fixes it. |
I am currently receiving the following error when running a Mongoose query in a test environment utilizing supertest and jest.
I am using Mongoose 5.6.9 with the code below:
The Query
The User Model
Can someone please help me figure out what's going on?
Thank you
The text was updated successfully, but these errors were encountered: