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

Bug: Cast error with empty string to a Boolean schema, happens on save and update #6859

Closed
drFabio opened this Issue Aug 14, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@drFabio
Copy link

drFabio commented Aug 14, 2018

Mongoose version: 5.2.8
Mongo version: mongod version: 3.6.6
Node version 8.11.1

Currently doing the snippet below fails on the creation and the update

const mongoose = require('mongoose');
const dbURL = 'SOME_URL';
const db = mongoose.createConnection(dbURL);
const schema = new mongoose.Schema({
  aField: Boolean
});
db.once('open', function() {
  const Example = db.model('Example', schema);

  const newEntry = new Example({aField: ''}); // change this to a bool to try th eupdate
  newEntry.save((err, saved) => {
    if (err) {
      console.log('Failed to save'): // this will happen
      throw err;
    }
    console.log('Saved entry');
    Example.findByIdAndUpdate(saved._id, {aField: ''}).then(() =>{
      console.log('Sucessfully updated')
    })
    .catch((err) => {
      console.error(err);  // this will happen
    })
  })
});

this throws:

Process crashed with: ValidationError: Example validation failed: aField: Cast to Boolean failed for value "" at path "aField"
    at new ValidationError (/home/runner/node_modules/mongoose/lib/error/validation.js:28:11)
    at model.Document.invalidate (/home/runner/node_modules/mongoose/lib/document.js:1898:32)
    at model.$set (/home/runner/node_modules/mongoose/lib/document.js:878:10)
    at model._handleIndex (/home/runner/node_modules/mongoose/lib/document.js:688:14)
    at model.$set (/home/runner/node_modules/mongoose/lib/document.js:645:22)
    at model.Document (/home/runner/node_modules/mongoose/lib/document.js:112:12)
    at model.Model (/home/runner/node_modules/mongoose/lib/model.js:69:12)
    at new model (/home/runner/node_modules/mongoose/lib/model.js:4334:13)
    at NativeConnection.<anonymous> (evalmachine.<anonymous>:10:20)
    at Object.onceWrapper (events.js:272:13)
    at NativeConnection.emit (events.js:180:13)
    at /home/runner/node_modules/mongoose/lib/connection.js:545:13
    at result (/home/runner/node_modules/mongodb/lib/utils.js:414:17)
    at executeCallback (/home/runner/node_modules/mongodb/lib/utils.js:406:9)
    at err (/home/runner/node_modules/mongodb/lib/operations/mongo_client_ops.js:285:5)
    at connectCallback (/home/runner/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)

Expected behaviour: Empty string get cast to false

@drFabio drFabio changed the title Bug: Cast error with empty string to a Boolean schema on save and findByIdAndUpdate Bug: Cast error with empty string to a Boolean schema, happens on save and update Aug 14, 2018

@lineus

This comment has been minimized.

Copy link
Collaborator

lineus commented Aug 14, 2018

The schematype doc’s have a section on what gets cast to true and false. According to the docs, empty string isn’t included in either true or false so it throws a cast error. Have you tried adding :

mongoose.Schema.Types.Boolean.convertToFalse.add('');
@lineus

This comment has been minimized.

Copy link
Collaborator

lineus commented Aug 14, 2018

Adding the empty string value to the convertToFalse Set works as expected:

6859.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.Schema.Types.Boolean.convertToFalse.add('');
const dbURL = 'mongodb://localhost:27017/gh6859';
const db = mongoose.createConnection(dbURL, { useNewUrlParser: true });
const schema = new mongoose.Schema({
  aField: Boolean
});

db.once('open', function () {
  const Example = db.model('Example', schema);

  const newEntry = new Example({ aField: '' });
  newEntry.save((err, saved) => {
    if (err) {
      console.log('Failed to save');
      throw err;
    }
    console.log('Saved entry');
    Example.findByIdAndUpdate(saved._id, { aField: '' }).then(() => {
      console.log('Sucessfully updated');
    })
      .catch((err) => {
        console.error(err);
      });
  });
});

Output:

issues: ./6859.js
Saved entry
Sucessfully updated
^C
issues:

@lineus lineus closed this Aug 14, 2018

@lineus lineus reopened this Aug 14, 2018

@drFabio

This comment has been minimized.

Copy link

drFabio commented Aug 16, 2018

Ok that makes sense.
Thansk a ton.

@drFabio drFabio closed this Aug 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment