Created
April 4, 2012 13:37
-
-
Save nagyv/2301095 to your computer and use it in GitHub Desktop.
The simplest Mongoose app to run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema, | |
ObjectId = Schema.ObjectId; | |
var myS = new Schema({ | |
name: {type: String, required: true} | |
}); | |
var My = mongoose.model('My', myS); | |
var my = new My({'name': 'bika'}); | |
my.save(function(err){ | |
console.log(err, 'saved'); | |
db.close(); | |
process.exit(); | |
}); | |
/** Using only this would never give you a save */ | |
//var db = mongoose.createConnection('mongodb://127.0.0.1:27017/test'); | |
/** You need these */ | |
mongoose.connect('mongodb://127.0.0.1:27017/test'); | |
var db = mongoose.connection | |
db.on('open', function(err){ | |
console.log('db open'); | |
if(err) throw err; | |
}); | |
db.on('error', function (err) { | |
console.log(err); | |
}); | |
// Just to avoid premature end of script | |
var express = require("express"), | |
app = express.createServer(); | |
app.listen(3000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason the above code does not save anything to the db, no callback is ever called.