mongoose: doc.validate is not a function

node_modules\mongoose\lib\schema\documentarray.js:104
      doc.validate({ __noPromise: true }, function(err) {
          ^

  TypeError: doc.validate is not a function

  - documentarray.js:104
    [hypetrain]/[mongoose]/lib/schema/documentarray.js:104:11

  - schematype.js:654 DocumentArray.SchemaType.doValidate
    [hypetrain]/[mongoose]/lib/schematype.js:654:22

  - documentarray.js:77 DocumentArray.doValidate
    [hypetrain]/[mongoose]/lib/schema/documentarray.js:77:35

  - document.js:1171
    [hypetrain]/[mongoose]/lib/document.js:1171:9

  - node.js:433 nextTickCallbackWith0Args
    node.js:433:9

  - node.js:362 process._tickCallback
    node.js:362:13

my schema;

var GameStatisticsSchema = new mongoose.Schema({
    game: { type: mongoose.Schema.ObjectId, ref: 'Game', required: true }, // associated game subverse.
    months: [
      { // monthly viewers
          epoch: { type: Number, required: true }, // entry time stamp
          weeks: [
            { // weekly viewers
                epoch: { type: Number, required: true }, // entry time stamp
                days: [
                  { // daily viewers
                      epoch: { type: Number, required: true }, // entry time stamp
                      hours: [
                        { // hourly viewers
                          epoch: { type: Number, required: true }, // entry time stamp
                          minutes: [
                            { // per minute statistics.
                                viewers: { type: Number },
                                channels: { type: Number },
                            }]
                      }]
                }]
          }]
    }]
});

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Hi, I run into this issue for the first time today.

I can’t tell exactly what is gonig on, if I find something I’ll come back.

2016-05-04T10:14:56.990+00:00, [error], message: Uncaught error: doc.validate is not a function stack: TypeError: Uncaught error: doc.validate is not a function
    at /home/api/node_modules/mongoose/lib/schema/documentarray.js:120:13
    at DocumentArray.SchemaType.doValidate (/home/api/node_modules/mongoose/lib/schematype.js:682:12)
    at DocumentArray.doValidate (/home/api/node_modules/mongoose/lib/schema/documentarray.js:81:35)
    at /home/api/node_modules/mongoose/lib/document.js:1212:9
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

so far I am not using doc.validate anywhere directly in my code.

and this is failing on a .save statement.

basically if i .save() a document, that has sub documents,and if one of those sub-documents is a plain object instead of a model, i get the error. does this make sense?

@vkarpov15

I have create a script to reproduce the problem.

'use strict';

var assert = require('assert');
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/gh3816');

var mySchema = new mongoose.Schema({
    items: [
        { month: Number, date: Date}
    ]
});

var Test = mongoose.model('test', mySchema);

var a = [
    {
        month : 0,
        date : new Date()
    },
    {
        month: 1,
        date : new Date()
    }];

Test.create({ items: a }, function(error, doc) {
    assert.ifError(error);
    Test.findById(doc._id).exec(function(error, doc) {
        assert.ifError(error);
        assert.ok(doc);
        doc.items[0] = {
            month: 5,
            date : new Date()
        };
        doc.save(function(error) {
            assert.ifError(error);
            console.log('done');
            process.exit(0);
        });
    });
});

This is reproducing the issue on my system.

doc.validate({__noPromise: true}, callback);
            ^

TypeError: doc.validate is not a function
    at ../node_modules/mongoose/lib/schema/documentarray.js:120:13
    at DocumentArray.SchemaType.doValidate (../node_modules/mongoose/lib/schematype.js:682:12)
    at DocumentArray.doValidate (../node_modules/mongoose/lib/schema/documentarray.js:81:35)
    at../node_modules/mongoose/lib/document.js:1212:9
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

@vkarpov15 Sorry for the delayed response. Example of changing array index:

var a = [
    {
        item0 : 0,
        item1 : 'Hello'
    },
    {
        item0: 1,
        item1: 'World'
    }];

a[0].item0 = 2;