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

Validation failed, when updating document after update mongoose version 6.8.0 to 6.8.1 #12821

Closed
2 tasks done
SirReiva opened this issue Dec 21, 2022 · 3 comments · Fixed by #12826
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@SirReiva
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.8.1

Node.js version

18.12.1

MongoDB server version

5.0.8

Typescript version (if applicable)

4.9.4

Description

When update mongoose from version 6.8.0 to 6.8.1 i get an error when i try to update a document.

Steps to Reproduce

const SocialAccountSchema = new Schema({
  _id: { type: String, required: true, _id: false },
  email: { type: String, required: true },
  provider: { type: String, required: true, enum: ["google", "facebook"] },
  username: { type: String },
  profilePic: { type: String },
});
const StudentSchema = new Schema({
	_id: { type: String, required: true, _id: false },
	email: { type: String, required: true, unique: true },
	name: { type: String, required: true },
	surname: { type: String, required: true },
	active: { type: Boolean, required: true },
	password: { type: String },
	profilePic: { type: String },
	socialAccounts: { type: [SocialAccountSchema] },
	courseIds: { type: [String] },
});
-------------------------------------------------------
async update(student: StudentModel): Promise<void> {
		try {
			const persistentStudent = this.toPersistence(student);

			const { _id, ...data } = persistentStudent;

			console.log(_id, data);

			await this.studentModel
				.findOneAndReplace({ _id }, data)
				.session(getCurrentSession())
				.exec();
		} catch (error) {
			console.log(error);
			throw error;
		}
}

---------------------------------------------------

console.log
945131ef-d0e3-43b7-8cdc-b85c238087aa {
email: 'cristinasmirnova@sbcglobal.io',
name: 'Anton',
surname: 'Rogers',
active: true,
password: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
profilePic: undefined,
socialAccounts: [],
courseIds: []
}

  at StudentMongoRepository.update (../src/profiles/student/infrastructure/repository/student-mongo.repository.ts:208:12)

console.log
Error: Validation failed
at ValidationError.inspect (C:\Users\gjjav\Documents\learnthis\node_modules\mongoose\lib\error\validation.js:50:26)
at formatValue (node:internal/util/inspect:806:19)
at inspect (node:internal/util/inspect:365:10)
at formatWithOptionsInternal (node:internal/util/inspect:2273:40)
at format (node:internal/util/inspect:2130:10)
at console.log (C:\Users\gjjav\Documents\learnthis\node_modules@jest\console\build\CustomConsole.js:141:41)
at StudentMongoRepository.update (C:\Users\gjjav\Documents\learnthis\apps\back-profiles\src\profiles\student\infrastructure\repository\student-mongo.repository.ts:215:12)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at StudentStandardActivateUseCase.execute (C:\Users\gjjav\Documents\learnthis\apps\back-profiles\src\profiles\student\application\use-cases\write\student-standard-activate.usecase.ts:36:3)
at StudentStandardActivateHandler.execute (C:\Users\gjjav\Documents\learnthis\apps\back-profiles\src\profiles\student\application\commands\student-standard-activate.handler.ts:26:3)
at StudentResolvers.student_activate (C:\Users\gjjav\Documents\learnthis\apps\back-profiles\src\profiles\student\infrastructure\graphql\student.resolver.ts:197:3)
at C:\Users\gjjav\Documents\learnthis\libs\shared-kernel\src\common\infrastructure\utils\transactional.decorator.ts:69:14
at StudentResolvers.WithTransaction.descriptor.value (C:\Users\gjjav\Documents\learnthis\libs\shared-kernel\src\common\infrastructure\utils\transactional.decorator.ts:68:4) {
errors: {
undefined: TypeError: Cannot convert undefined or null to object
at Function.keys ()
at castUpdate (C:\Users\gjjav\Documents\learnthis\node_modules\mongoose\lib\helpers\query\castUpdate.js:88:30)
at model.Query._castUpdate (C:\Users\gjjav\Documents\learnthis\node_modules\mongoose\lib\query.js:5126:10)
at model.Query. (C:\Users\gjjav\Documents\learnthis\node_modules\mongoose\lib\query.js:3846:27)
at model.Query._wrappedThunk [as _findOneAndReplace] (C:\Users\gjjav\Documents\learnthis\node_modules\mongoose\lib\helpers\query\wrapThunk.js:29:8)
at C:\Users\gjjav\Documents\learnthis\node_modules\kareem\index.js:494:25
at processTicksAndRejections (node:internal/process/task_queues:77:11)
},
_message: 'Validation failed'
}

Expected Behavior

Not get errors on update a valid document

@Uzlopak Uzlopak changed the title Validation failed, when updating document after update mongoose version 8.6.0 to 8.6.1 Validation failed, when updating document after update mongoose version 6.8.0 to 6.8.1 Dec 21, 2022
@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Dec 21, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const {Schema} = mongoose;

const SocialAccountSchema = new Schema({
  _id: { type: String, required: true, _id: false },
  email: { type: String, required: true },
  provider: { type: String, required: true, enum: ["google", "facebook"] },
  username: { type: String },
  profilePic: { type: String },
});
const StudentSchema = new Schema({
	_id: { type: String, required: true, _id: false },
	email: { type: String, required: true, unique: true },
	name: { type: String, required: true },
	surname: { type: String, required: true },
	active: { type: Boolean, required: true },
	password: { type: String },
	profilePic: { type: String },
	socialAccounts: { type: [SocialAccountSchema] },
	courseIds: { type: [String] },
});

const Student = mongoose.model('Student', StudentSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  const _id = '945131ef-d0e3-43b7-8cdc-b85c238087aa';
  const data = {email: 'test@localhost.com',
  name: 'test',
  surname: 'testerson',
  active: true,
  password: 'aaaaaaaaaaaaaaaaaaaa',
  profilePic: undefined,
  socialAccounts: [],
  courseIds: []};
  await Student.create({
    _id: '945131ef-d0e3-43b7-8cdc-b85c238087aa',
    email: 'test@localhost.com',
    name: 'test',
    surname: 'testerson',
    active: true,
    password: 'aaaaaaaaaaaaaaaaaaaa',
    profilePic: undefined,
    socialAccounts: [],
    courseIds: []
  });

  await Student.findOneAndReplace({ _id }, {data}).session()
  console.log('done');
}

run();

@SirReiva
Copy link
Author

SirReiva commented Dec 21, 2022

@IslandRhythms
you wrap replacement with "data" key { data }
With this code i get same error if not wrap the replacement

const mongoose = require("mongoose");
const { MongoMemoryReplSet } = require("mongodb-memory-server");

const { Schema } = mongoose;

const SocialAccountSchema = new Schema({
  _id: { type: String, required: true, _id: false },
  email: { type: String, required: true },
  provider: { type: String, required: true, enum: ["google", "facebook"] },
  username: { type: String },
  profilePic: { type: String },
});
const StudentSchema = new Schema({
  _id: { type: String, required: true, _id: false },
  email: { type: String, required: true, unique: true },
  name: { type: String, required: true },
  surname: { type: String, required: true },
  active: { type: Boolean, required: true },
  password: { type: String },
  profilePic: { type: String },
  socialAccounts: { type: [SocialAccountSchema] },
  courseIds: { type: [String] },
});

const Student = mongoose.model("Student", StudentSchema);

async function run() {
  const mongo = await MongoMemoryReplSet.create({
    replSet: { count: 1, dbName: "testdb" },
  });
  await mongo.waitUntilRunning();
  await mongoose.connect(mongo.getUri());
  await mongoose.connection.dropDatabase();
  const _id = "945131ef-d0e3-43b7-8cdc-b85c238087aa";
  const data = {
    email: "test@localhost.com",
    name: "test",
    surname: "testerson",
    active: true,
    password: "aaaaaaaaaaaaaaaaaaaa",
    profilePic: undefined,
    socialAccounts: [],
    courseIds: [],
  };
  await Student.create({
    _id: "945131ef-d0e3-43b7-8cdc-b85c238087aa",
    email: "test@localhost.com",
    name: "test",
    surname: "testerson",
    active: false,
    password: "aaaaaaaaaaaaaaaaaaaa",
    profilePic: undefined,
    socialAccounts: [],
    courseIds: [],
  });

  await Student.findOneAndReplace({ _id }, data).exec();
  //await Student.findOneAndReplace({ _id }, { data }).exec();
  console.log("done");
}

run();

if findOneAndReplace is executed with { data }
this is db final state(only id persist)
image

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Dec 21, 2022
@IslandRhythms
Copy link
Collaborator

Ty

const mongoose = require('mongoose');

const {Schema} = mongoose;

const SocialAccountSchema = new Schema({
  _id: { type: String, required: true, _id: false },
  email: { type: String, required: true },
  provider: { type: String, required: true, enum: ["google", "facebook"] },
  username: { type: String },
  profilePic: { type: String },
});
const StudentSchema = new Schema({
	_id: { type: String, required: true, _id: false },
	email: { type: String, required: true, unique: true },
	name: { type: String, required: true },
	surname: { type: String, required: true },
	active: { type: Boolean, required: true },
	password: { type: String },
	profilePic: { type: String },
	socialAccounts: { type: [SocialAccountSchema] },
	courseIds: { type: [String] },
});

const Student = mongoose.model('Student', StudentSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  const _id = '945131ef-d0e3-43b7-8cdc-b85c238087aa';
  const data = {email: 'test@localhost.com',
  name: 'test',
  surname: 'testerson',
  active: true,
  password: 'aaaaaaaaaaaaaaaaaaaa',
  profilePic: undefined,
  socialAccounts: [],
  courseIds: []};
  await Student.create({
    _id: '945131ef-d0e3-43b7-8cdc-b85c238087aa',
    email: 'test@localhost.com',
    name: 'test',
    surname: 'testerson',
    active: true,
    password: 'aaaaaaaaaaaaaaaaaaaa',
    profilePic: undefined,
    socialAccounts: [],
    courseIds: []
  });

  await Student.findOneAndReplace({ _id }, data).session()
  console.log('done');
}

run();

@vkarpov15 vkarpov15 added this to the 6.8.2 milestone Dec 22, 2022
vkarpov15 added a commit that referenced this issue Dec 23, 2022
fix(query): fix unexpected validation error when doing findOneAndReplace() with a nullish value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants