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
Subdocument validation error - Cast to Array failed for value - .save() #8472
Comments
[WORKAROUND] I have changed the schema to :
And it worked (with the same data). Here is the final data on the database : |
The below script succeeds without throwing any errors, please modify the below script to demonstrate your issue. 'use strict';
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const schema = Schema({
name: Schema({
description: String,
translations: [Schema({
idLanguage: {
type: Schema.Types.ObjectId,
ref: 'Language',
},
value: String
})]
})
});
const Model = mongoose.model('Test', schema);
await Model.create({ name: { description: 'Test' } });
const doc = await Model.findOne();
doc.name.translations.push({
"idLanguage": "aaaaa0000000000000000000",
"value": "Français"
});
await doc.save();
console.log('Done');
} Also, it looks like you're using some sort of transpiler, can you please clarify what transpiler you're using? |
First, you are right I'm using a transpiler: Babel for my main project and typescript for a small package I use to interact with the database (schema handler). I've made a lot of different try and I have state that :
My conclusion : It's 100% a transpiler issue, I got no issue when I perform every mongoose action at the same location. Thank you for your help, I think this issue is VERY specific. Indeed my main project is migrating to typescript so I am using babel, when my lower packages are already in typescript (compilation target setted to es5). I'll set my Schema to Schema.Type.Mixed until I finish the migration of my whole project into typescript. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
Cannot
.save()
changes made on a document due to validation error.If the current behavior is a bug, please provide the steps to reproduce.
I have the following schema :
I'm getting a document from the database using a
.find()
request, then push new data into thename.translations
array and.save()
.I got the following runtime error :
I've printed the data I'm trying to save :
It should work.
PS: The exact same code worked in previous version of mongoose 5.6.x
What is the expected behavior?
The document get successfully saved
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: