Masiar
Masiar

Reputation: 21352

Setting up Mongoose with provided demo not working

Hey all, I'm very new to mongodb, mongoose and node.js. I would like to create a little demo to see how mongoose works. After installing (and testing the correctes of) node.js I downloaded mongoose and tried the following code (also provided on the mongoose website):

require.paths.unshift('vendor/mongoose');
var mongoose = require('mongoose').Mongoose;

mongoose.model('User', {

properties: ['first', 'last', 'age', 'updated_at'],

cast: {
  age: Number,
  'nested.path': String
},

indexes: ['first'],

setters: {
    first: function(v){
        return this.v.capitalize();
    }
},

getters: {
    full_name: function(){ 
        return this.first + ' ' + this.last 
    }
},

methods: {
    save: function(fn){
        this.updated_at = new Date();
        this.__super__(fn);
    }
},

static: {
    findOldPeople: function(){
        return this.find({age: { '$gt': 70 }});
    }
}

});

var db = mongoose.connect('mongodb://localhost/db');

var User = db.model('User');

var u = new User();
u.name = 'John';
u.save(function(){
sys.puts('Saved!');
});

User.find({ name: 'john' }).all(function(array){

});

The problem is that when I run node myfile.js I got the following error:

node.js:181
    throw e; // process.nextTick error, or 'error' event on first tick
    ^
Error: Cannot find module 'mongoose'
at Function._resolveFilename (module.js:320:11)
at Function._load (module.js:266:25)
at require (module.js:364:19)
at Object.<anonymous> (/my/path/to/mongoose+node test/myfile.js:2:16)
at Module._compile (module.js:420:26)
at Object..js (module.js:426:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at Array.<anonymous> (module.js:439:10)
at EventEmitter._tickCallback (node.js:173:26)

Now, I have to say again that I'm really new to this, so my folder called "mongoose+node test" has inside only the mongoose folder which contains a bunch of JavaScript files, and of course myfile.js. Am I maybe missing something?

Upvotes: 0

Views: 1298

Answers (2)

neeebzz
neeebzz

Reputation: 11538

In the new version, you don't need to use .Mongoose .

Just replace the following :

var mongoose = require('mongoose').Mongoose;

with:

var mongoose = require('mongoose')

Upvotes: 1

Raynos
Raynos

Reputation: 169391

It can't find mongoose. The easiest way to deal with this is to install it through npm.

To intall npm:

curl http://npmjs.org/install.sh | sh

To install mongoose:

npm install mongoose

You also have to download and install mongoDB and start a mongoDB server.

The unix quickstart will help you install, run and test mongoDB.

Your main problem is that require.paths should not be edited. You should be requiring an url directly or going through a package system. In the nodejs docs it states require.paths should be avoided.

Personally I would recommend you stick to npm as it's the de-factor standard.

Upvotes: 3

Related Questions