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

Model with boolean property doen't work properly #7

Closed
lkmrus opened this issue Jul 13, 2021 · 2 comments
Closed

Model with boolean property doen't work properly #7

lkmrus opened this issue Jul 13, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@lkmrus
Copy link

lkmrus commented Jul 13, 2021

The schema will not work properly

const mongoose = require('mongoose');
const mongooseConnection = await mongoose.createConnection(MONGODB_URL, { useNewUrlParser: true });

const authorSchema = mongoose.Schema({
    firstName: String,
    lastName: String,
    aGoodMan: Boolean,
    biography: String,
    created: { type: Date, default: Date.now }
});

You can save a doc with the property aGoodMan equal to true.
It will not be possible to change it to false then.

The reason is in the method async apiPut of class DefaultModelMethods.

async apiPut(data) {
	//// this points to document (schema.methods.)
	this.schema.eachPath((pathname) => {
		if (data[pathname]) {
			this[pathname] = data[pathname];
		} else if (data[pathname] === null) {
			this[pathname] = undefined;
		}
	});

	await this.save();
}

It's better to change the method of checking value to something like this:

async apiPut(data) {
	//// this points to document (schema.methods.)
	this.schema.eachPath((pathname) => {
		if (data[pathname] !== undefined) {
			this[pathname] = data[pathname];
		} else if (data[pathname] === null) {
			this[pathname] = undefined;
		}
	});

	await this.save();
}
@jeka-kiselyov
Copy link
Owner

Good point, @lkmrus

I will take care of this.

@jeka-kiselyov jeka-kiselyov self-assigned this Jul 14, 2021
@jeka-kiselyov jeka-kiselyov added the bug Something isn't working label Jul 14, 2021
@jeka-kiselyov
Copy link
Owner

Fixed with 80124275e1b15275e6295b4cdd91b41c599a502e

v updated to 1.1.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants