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
TypeError: Cannot read property 'findOne' of null with capped collection #8566
Comments
I suspect this is because of the Starting on mongoose 5.0, mongoose buffers all commands until a connection is established. I think that causes the underlying mongodb driver to attempt to execute a query on a collection that it doesn't know about yet. Will add a reproduction test ASAP. Meanwhile, try removing the |
Thanks for the answer. Without the Unfortunately, I use the |
Could you elaborate on that? I think this behavior is by design, if you choose to not use bufferCommands, then you'll need to carry the burden of waiting for a connection to be established before running code the tries to use the database. However, here's a reproduction script: const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');
mongoose.connect('mongodb://127.0.0.1:27017/test', { useNewUrlParser: true });
const userSchema = new Schema({ name: String }, { bufferCommands: false });
const User = mongoose.model('User', userSchema);
async function run () {
const user = await User.create({ name: 'Hafez' });
assert.equal(user.name, 'Hafez');
}
run().catch(console.error); which outputs:
Tested with: |
Sure. Sorry if I wasn't specific in my issue. It's not a problem for me to wait for the connection establishment - as you can see, I was The problem is about using Regarding the disabled To eliminate the versions inconsistencies - I've faced with the Sorry if I'm wrong somewhere. Thanks in advance. |
I took a closer look and I'm going to have to say that this is expected behavior. With
const Test = new Schema(
{
author: ObjectId,
title: String
},
{
bufferCommands: false, autoCreate: false
}
);
const testsModel = mongoose.model("test", Test);
await testsModel.createCollection({ capped: true, size: 1000000, max: 100 });
await mongoose.connect("mongodb://localhost:27017/test-db", {
useUnifiedTopology: true,
useNewUrlParser: true,
autoCreate: true
});
await mongoose.connection.dropDatabase();
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
const Test = new Schema(
{
author: ObjectId,
title: String
},
{
bufferCommands: false, capped: { size: 1000000, max: 100 }
}
);
const testsModel = mongoose.model("test", Test);
await testsModel.init(); We'll make some changes to the docs to make this more clear. |
…false` with capped collections Re: #8566
Thanks for the help. |
Do you want to request a feature or report a bug?
I want to report a bug.
What is the current behavior?
When I trying to use the
findOne
method immediately after themodel
creation I get the exception:Please note that I face this exception only if I use these options simultaneously in the
schema
:If the current behavior is a bug, please provide the steps to reproduce.
There is the example code to reproduce the bug:
If I add the delay (e.g. 100 milliseconds) between the
mongoose.model
andfindOne
calls the problem disappears. But this doesn't look like a production-ready solution. Can you advice me more stable workaround, please?Also seems that the problem does not depend on collection existence in the database.
What is the expected behavior?
I should be able to call
findOne
immediately after themodel
call or I should have a possibility to check if the compiledmodel
is ready to use.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: v10.15.3
Mongoose: 5.8.11
MongoDB: 3.4
The text was updated successfully, but these errors were encountered: