Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.96k stars 3.84k forks source link

MongoError: the limit must be positive #5049

Closed vkupar closed 7 years ago

vkupar commented 7 years ago

BUG

{ MongoError: the limit must be positive at Function.MongoError.create (/var/app/current/node_modules/mongodb-core/lib/error.js:31:11) at /var/app/current/node_modules/mongodb-core/lib/connection/pool.js:483:72 at authenticateStragglers (/var/app/current/node_modules/mongodb-core/lib/connection/pool.js:429:16) at Connection.messageHandler (/var/app/current/node_modules/mongodb-core/lib/connection/pool.js:463:5) at Socket. (/var/app/current/node_modules/mongodb-core/lib/connection/connection.js:317:22) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at Socket.Readable.push (_stream_readable.js:134:10) at TCP.onread (net.js:548:20) name: 'MongoError', message: 'the limit must be positive', ok: 0, errmsg: 'the limit must be positive', code: 15958, codeName: 'Location15958' }

Mongoose: 4.7.5 MongoDB: 3.4 Node.js: 6.9.1

I'm trying to make [Find, Populate, Exec] I have two code, old and new. when I changed old code with new, this error appeared. In old version, "population" made by manually, with two search and in new code I use populate method to do this... I can't upgrade mongoose, because of mongoose's integrated promises is deprecated and now I don't have time to rewrite full project...

sobafuchs commented 7 years ago

Do you have a more helpful repro script?

vkupar commented 7 years ago

I used ES6 promises incorrectly and it was the problem. My code looked like: Model .findOne() .then() .catch()

Now I'm using .exec() method only and it works without errors

sobafuchs commented 7 years ago

That isn't an incorrect use of promises Do you have more details?

vkupar commented 7 years ago

@varunjayaraman No. I removed .then() promise and now it works very good.

sobafuchs commented 7 years ago

@vatex I'm saying that you shouldn't have to remove .then(). Can you show me how you used it?

vkupar commented 7 years ago
MyModel
.findOne({ user: 'admin@gmail.com' }, 'email password createdAt')
then((result) => {
    console.log(result);
})
.catch((error) => {
    console.log(error);
});
sobafuchs commented 7 years ago

chaining .then() works for me:

const mongoose = require('mongoose');
const co = require('co');
mongoose.Promise = global.Promise;

const GITHUB_ISSUE = `gh-5049`;

exec()
  .then(() => {
    console.log('Program successful');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${ error }\n${ error.stack }`);
    process.exit(2);
  });

function exec() {
  return co(function*() {
    const db = mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

    const schema = new mongoose.Schema({
      email: String,
      password: String
    }, { timestamps: true });

    const Model = db.model('Model', schema);
    yield Model.remove({});

    const doc = yield Model.create({
      email: 'test@test.com',
      password: 'test'
    });

    return Model
      .findOne({ email: 'test@test.com' }, 'email password createdAt')
      .then(result => {
        console.log('result', result);
      });
  });
}
vkupar commented 7 years ago

@varunjayaraman Yes but I missed this code: mongoose.Promise = global.Promise; and maybe it was problem

sobafuchs commented 7 years ago

Ok sounds good