title: Issue with mongodb's collection.find() when used in an express app.js file
body: <p>Just a little background: I'm quite new to backend code (node js, express js) so bear with me... I'm working on building a website and a Shopify app using express js and mongodb. The website and Shopify app both need to talk to the same database (in this case I've called it tbapp). Since I've never built a Shopify app before I decided to try to follow the express js code written in <a href="https://github.com/Sovi-Creative/shopify-embedded-app-blog-example" rel="nofollow">this</a> git hub repo. I noticed that this example app wasn't using a routes directory, so without thinking much of it, I decided not to use one as well, and put all of my "get" and "post" requests in my app.js file. However unlike my app, the example app isn't storing any information in a database...</p>
  
  <p>Now for the actual question: I'm trying to find out if a store owner entered a matching key into their store admin– so I'm using "collection.find". This works fine when the store owner enters the correct key, however whenever they enter a key that's not in my database, the function returns an error– even though I've specified that when "collection.find" cannot find a document it should render a page with a local variable passed to it. Below is the relevant code in my app.js file. Do you think that not using an index.js file in routes is causing this problem? This is the only explanation I can think of, since I'm literally doing the same thing in my website, but the only difference is that my website code is using routes.</p>
  
  <p>This is the initial code in my app.js file:</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var crypto= require("crypto");
  var querystring= require("querystring");
  var request= require("request");
  var session= require("express-session");
  
  var mongo= require("mongodb");
  var monk= require("monk");
  var db= monk("localhost:27017/tbapp");
  
  
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.engine("html", require("ejs").__express);
  //changed renderFile
  
  app.set('view engine', 'html');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(session({secret: 'somethingsecret', saveUninitialized: true, resave: true}));
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use(function(req, res, next){
     req.db= db;
  
  
      next();
  });
  </code></pre>
  
  <p>This is the problem code:</p>
  
  <pre><code>app.post("/enter-store-key", function(req, res) {
  
      var db= req.db;
      var key= req.body.storeKey;
      var collection= db.get("businessOwners");
  
      console.log(key);
      collection.find({_id: key}, {}, function(err, doc) {
          if(err) {
              res.send("There was an error");
          } else {
              if(doc.length&gt;0) {
                 res.render("widg-index", {
                 api_key: config.shopify_api_key,
                 shop: config.shop,
                 title: "Home",
                 valid_key: "yes"
                 }); 
              } else {
                 res.render("widg-index", {
                 api_key: config.shopify_api_key,
                 shop: config.shop,
                 title: "Home",
                 valid_key: "no"
                 }); 
  
          }
          }
      }); 
  
      //WHEN STORE OWNER DOESN'T INPUT A CORRECT KEY I GET AN ERROR PAGE??? IF FUNCTION "FIND" CANNOT FIND A DOCUMENT WITH SPECIFIED QUERY IT RETURNS AN ERROR?? WHYYYYYY????!!
  
  });
  </code></pre>
  
  <p>Thank you to anyone who helps me with this. I've been struggling for awhile now, so any and all advice would be much appreciated!</p>