question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

required: true on objectId causes the validation to fail

See original GitHub issue

I have a Schema that contains a Ref to another one. If I mark it as required, to validation fails. Without this attributes, everything go fine, including the ref.

Here is the actual Schema

answerChoiceSchema = new schema {
  propositionId: { type: objectId, required: true},
  dispo: { type: String, required: true, enum: ['yes', 'no']}
}

and an extract of my code

//c comes directly from a POST key/value array which just fine
choice = new Choice
choice.propositionId = c.propositionId
choice.dispo = c.dispo
answer.choices.push choice

I do not know if this is of any help to find the issue but “choice” data comes at 3rd level of nesting (parent->answers->choices)

What can I do about it ?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
sam13591980commented, Oct 23, 2017

I have the same problem

0reactions
azat-cocommented, Sep 23, 2012

I think I found mistake in my code after writing short example. I was passing options to create method but it treated them as second set of argument and of course validation failed. Thank you!

In case somebody else need it, this doesn’t work:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var PostModel = new Schema ({
    author: {
        _id: {
            type: Schema.Types.ObjectId,
            ref: "User",
            required: true
        },
        name: String
    },
    text: String
});

var UserModel = new Schema ({
    name: String
});

var dbUrl = process.env.MONGOHQ_URL || "mongodb://@127.0.0.1:27017/test";

var connection = mongoose.createConnection(dbUrl);
var User = connection.model('User', UserModel, "users");
var Post =  connection.model('Post', PostModel, "posts");
var user;
User.create({
    name: "John"
}, function (err, obj){
    if (err) {
        console.log(err)
    } else {
        console.log(obj);
        user = obj;
        Post.create({
            author:{
                _id: user._id
            },
            text: "Blah blah blah blah blah"
        },{safe:true}, function (err, obj){
            if (err) {
                console.log(err) 
            } else {
                console.log(obj);
            }
        });
        // var post = new Post();
        // post.
    }
})

But this works

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var PostModel = new Schema ({
    author: {
        _id: {
            type: Schema.Types.ObjectId,
            ref: "User",
            required: true
        },
        name: String
    },
    text: String
});

var UserModel = new Schema ({
    name: String
});

var dbUrl = process.env.MONGOHQ_URL || "mongodb://@127.0.0.1:27017/test";

var connection = mongoose.createConnection(dbUrl);
var User = connection.model('User', UserModel, "users");
var Post =  connection.model('Post', PostModel, "posts");
var user;
User.create({
    name: "John"
}, function (err, obj){
    if (err) {
        console.log(err)
    } else {
        console.log(obj);
        user = obj;
        Post.create({
            author:{
                _id: user._id
            },
            text: "Blah blah blah blah blah"
        }, function (err, obj){
            if (err) {
                console.log(err) 
            } else {
                console.log(obj);
            }
        });
    }
})


Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do I have a fail validation with mongodb? - Stack Overflow
Okay, I got the solution, the problem was the additional rule Properties. So if it's on false, you have to add the _id...
Read more >
Mongoose v6.8.2: API docs
Returns true if the given value is a Mongoose ObjectId (using instanceof ) or if the given value is a 24 character hex...
Read more >
How To Use Schema Validation in MongoDB - DigitalOcean
In MongoDB, the database engine feature that makes it possible to apply constraints on the document structure is called Schema Validation ...
Read more >
Failed to parse field of type in document with id ''. - Opster
This error is actually quite simple to replicate. You only need to make sure you are somehow not respecting Elasticsearch's constraints for the...
Read more >
Schema Validation — MongoDB Manual
What Happens When a Document Fails Validation ... By default, when an insert or update operation would result in an invalid document, MongoDB...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found