matt.condit matt.condit - 2 months ago 6
Node.js Question

Mongoose (express, node, mongo) scope issue with findOne()

I'm having an issue with what I believe is the scope of a variable in mongoose. My code is this:

var blogUserId;
blogs.forEach(function(blog, index) {
User.findOne({'username': blog.username}, function(err, user) {
blogUserId = user._id;
console.log(blogUserId);
});
console.log(blogUserId);

Blog.find({'title': blog.title}, function(err, blogs) {
if (!err && !blogs.length) {
console.log(blogUserId);
Blog.create({title: blog.title, author: blogUserId, body: blog.body, hidden: blog.hidden});
}
if (err) {
console.log(err);
}
});
});


This is part of a seed file just for development, but I'm rather confused why it wouldn't be working.
blogs
is just an array of objects to load into the collection. I've searched on all similar answers but I haven't found a correct answer that would explain this.

rsp rsp
Answer

Your blogUserId is not set when your Blog.find() is called. You'd have to nest it differently, something like this:

var blogUserId;
blogs.forEach(function(blog, index) {
  User.findOne({'username': blog.username}, function(err, user) {
    blogUserId = user._id;
    console.log(blogUserId);

    Blog.find({'title': blog.title}, function(err, blogs) {
      if (!err && !blogs.length) {
        console.log(blogUserId);
        Blog.create({title: blog.title, author: blogUserId, body: blog.body, hidden: blog.hidden});
      }
      if (err) {
        console.log(err);
      }
    });

  });

});

I haven't tested it so I'm not sure that there are no other errors in your code but was definitely a problem that you were calling Blog.find that expected the blogUserId to be set possibly before it was set in the User.findOne callback.

It can be written in a more readable way with named callbacks.

When working in Node you need to keep in mind that you are working in an asynchronous environment.