2

I'm working on a project that uses a node/express API and Mongo for storage. I have a function that tries to retrieve data from the storage using the code in the screenshot below. My understanding of async/await is that at the point of await, code execution will pause and proceed when the promise is resolved.

However, the data returned by the function (in the screenshot) is always null, although, the record is there in the db. [The slug is also passed correctly.]
I am starting to believe I am missing something regarding the concept of async/await. Could anyone please assist me with this? Am I doing something wrong here?

The calling function is as follows:

 async create(req, res, next) {

    debug(chalk.blue(`*** Create RSVP`));
    console.log(req.body.event); //event is defined and matches db
    
    const event = await Event.findBySlug(req.body.event);
    console.log(event); // logs null here
 }

Called function:

async function findBySlug(slug) {
    return await Model.findOne({ slug: slug })
    .populate('user')
    .populate('category')
    .exec();
}
d-cubed
  • 1,034
  • 5
  • 30
  • 58
Tatenda
  • 679
  • 2
  • 10
  • 24
  • 4
    "Am I doing something wrong here?" - [Posting code as image](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) is pretty bad. :P – Amadan Aug 22 '19 at 09:00
  • Another helpful thing - If there's no error, `function log(x) { console.log(x); return Promise.resolve(x); }` and sprinkling your promise with `.then(log).` to see exactly where it's breaking down. Maybe your `Model.findOne` doesn't find one at all. – Amadan Aug 22 '19 at 09:06
  • are you sure that there is matching slug in the database? also you don't need to ```return await Model.findOne``` because your await call is happening at the function invocation. – Dan Starns Aug 22 '19 at 09:06
  • 3
    If `event` is actually `null` it indicates the problem is not with `async/await`. It indicates there is no model matching the slug. Query you DB to check there is something matching the criteria. – Yury Tarabanko Aug 22 '19 at 09:08
  • 3
    Chances are that the database does not have a matching result, async/await code is working properly. – Ganesh Karewad Aug 22 '19 at 09:10

2 Answers2

1

I have run your code, findBySlug should be working fine. below is sample code for you.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/database-name', {useNewUrlParser: true});

const UserSchema = new mongoose.Schema({
    username: String
  })

const CategorySchema = new mongoose.Schema({
    name: String
  })

const PostSchema = new mongoose.Schema({
    content: String,
    author: {
      type: ObjectId,
      ref: 'User'
    },
    category: {
      type: ObjectId,
      ref: 'Category'
    }
  })

const Post = mongoose.model('Post', PostSchema, 'posts');
const User = mongoose.model('User', UserSchema, 'users');
const Category = mongoose.model('Category', CategorySchema, 'categories');

async function findBySlug() {

    return await Post.findOne({ content: "content name" })
    .populate('author')
    .populate('category')
    .exec();

}

 (async function run() {

    const event = await findBySlug();
    console.log(event); // logs not null here

 }())
Mir Nawaz
  • 304
  • 1
  • 9
0

updating your findBySlug method like this will be enough.

function findBySlug(slug) {
    return Model.findOne({ slug: slug })
    .populate('user')
    .populate('category')
    .exec();

}

kisor
  • 464
  • 5
  • 22