MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Mongoose hangs, no result on schema.find

I use mongoose to connect to mongodb, then I use
`.find({ author: req.author }).then(result => {`

But this gives nothing, it just freezes on here or something? What is wrong?

Could be syntax issue
Does the collection/field name match with what you have on DB
Is it working with any other tool like Compass or shell

By default, mongoose will buffer all of your queries until a connection is made. FAQ Link Here and check out the operation buffering docs here

It works in a other project, the same connection string and schema

if i log the connection number (forgot what it was) I get a 1 which means connected

If adding mongoose.set('bufferCommands', false) before mongoose.connect() doesn’t result in your query immediately throwing an error, then you’ll need to share an example of what you’re doing in a way that’s reproducible so someone can help you.

here’s an example generic script that I frequently use as a starting point:

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

const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const DB = 'robin';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = {
  useNewUrlParser: true,
  useUnifiedTopology: true
};

const schema = new Schema({
  name: String
});

const Test = mongoose.model('test', schema);

const test = new Test({ name: 'test1' });

async function run() {
  await mongoose.connect(URI, OPTS);
  await connection.dropDatabase();
  const doc = await Test.create(test);
  assert.ok(doc);
  await connection.close();
}

run();