2

I'd like an environment where I can get a solid grasp of how CRUD operations work. Up to this point, I've been using views to see how the data looks like but with the obvious inflexibility, this approach isn't that insightful -- it's something like driving in the dark.

Now I want to be able to work with the data in MongoDB via the functionality offered by Mongoose so that I see what actually happens when I do this or that, subdocuments, population, that sort of stuff. Initially, I had this code in a script.js file:

var mongoose = require('mongoose');
// ...
// define schemas
// try some crazy things
// ...
console.log(results);

And then I attempted to execute that code:

node script.js

Which didn't work. I then tried:

mongo load('script.js')

This time, I got an error, of course -- require() is not defined. Facepalm.

Hopefully that gives you an idea of what I'm trying to do. I've since tried other methods that didn't seem to work.

Please advise.

UPDATE

And here's my script.js file:

var Hero = require('./hero-model');
// This hero-model.js file defines the schema
// and exports its functionality. I've used
// this model to successfully CRUD data
// via Express. You can ignore the code above
// where I stated require('mongoose') because I was
// merely simplifying. Running this script causes
// the command to terminate silently.

var getHeroes = function() {

    Hero.find(function(err, heroes) {
        if (err) {
            console.log(err);
        }
        console.log(heroes);
    });
};

getHeroes();
Zero
  • 717
  • 6
  • 21
  • I checked that also. The app is indeed connected to its database. – Zero Oct 08 '14 at 20:22
  • Didn't I just mention that when I use Express, I can successfully get the docs to be output to the views? That means my database connection and collection is set up just right. – Zero Oct 08 '14 at 20:32
  • Found the problem and solved it. I had to move my scripts to a new app which did not have Express installed as a dependency. Actually, Mongoose is the only dependency defined in this new app's package.json file. And that worked. – Zero Oct 08 '14 at 21:57

2 Answers2

0

You're trying to run a node script as a mongo shell script. To run it as a regular node script, simply use:

node script.js
jmar777
  • 38,796
  • 11
  • 66
  • 64
  • @FourEyedHawk What happens when you try? Are you running the command from the same directory as `script.js`, and do you have all the necessary modules installed? – jmar777 Oct 08 '14 at 19:46
  • It's the same directory within the same app where I'm outputting data to the views. The console should output the same data but that's not the case -- the command silently terminates. – Zero Oct 08 '14 at 19:51
  • Can you show your entire file, then? Silent termination is... irregular. – jmar777 Oct 08 '14 at 19:59
  • I've updated my question to include the script.js file. – Zero Oct 08 '14 at 20:15
  • Try logging some debug info. E.g., `console.log("about to call getHeroes();")`. Something to just verify that the fundamentals of running the script are working. – jmar777 Oct 08 '14 at 20:31
  • Ah. I found the problem. I moved all my scripts to a new app which doesn't have Express installed as a dependency. Worked like a charm. Thanks for your time, though. – Zero Oct 08 '14 at 21:49
  • @FourEyedHawk Honestly that doesn't even make sense (at least not from the code you posted). Having express hanging around in the `node_modules` folder, as long as you never `require()` it, has no effect on your other code. – jmar777 Oct 08 '14 at 22:32
  • My bad. I should've picked my words more carefully. I meant that the first time, I used the Express generator -- `express heroes-app` -- to prepare my app files. This time I didn't. – Zero Oct 09 '14 at 09:07
0

You can run node to run nodejs without expressJs but I think we should install expressJs to cope with database