Connection Mongoose

76 views
Skip to first unread message

rmo...@gmail.com

unread,
Jan 15, 2014, 12:32:16 PM1/15/14
to tot...@googlegroups.com
Hi guys,

I have database.js with

framework.mongoose = require('mongoose');

framework.database = function() {

var mongoose = require('mongoose');
mongoose.connect(framework.uri, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + framework.uri + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + framework.uri);
}
});

};

and created user.js

// Carregar models
var User = require('../models/User')(framework.mongoose);

exports.install = function(framework) {
framework.route('/user/new', new_user);
framework.route('/user/save', save_user, ['xhr', 'post']);
};

function new_user(){
var self = this;
self.view('new_user');
}

function save_user(){


var self = this;
var model = self.post;


// Salvar
var user = User({
name: model.name,
login: model.name,
email: model.email,
passw: model.passw
});

console.info(user);

user.save();

self.json({ r: true });

};

but my User.js in console.log(mongoose) is undefined.
What will i do to solve?

module.exports = function(mongoose){

console.log(mongoose);
var userSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: true
},
email: {
type: String,
trim: true,
required: true
},
login: {
type: String,
required: true
},
passw: {
type: String,
required: true
}
});

var schemaOptions = {
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
}
};

var User = mongoose.model('User', userSchema, schemaOptions);


return {
User: User
}
};

A Moore

unread,
Jan 17, 2014, 1:50:27 AM1/17/14
to tot...@googlegroups.com, rmo...@gmail.com
Any success with this, I am working on integrating mongoose as well. If you have a repo we could collaborate.

Peter Širka

unread,
Jan 17, 2014, 2:51:14 AM1/17/14
to tot...@googlegroups.com, rmo...@gmail.com
Hi,
all is bad declared.

1. define mongoose as global variable:

global.mongoose = require('mongoose');

 In all scripts you can use mongoose variable without require.

2. models you can define in definitions, example:

/definitions/model.user.js
/definitions/model.product.js
/definitions/model.order.js


3. you can use total.js schemas:

builders.schema('user', function() { return new mongoose.Schema({ ..... }); });

In controller or elsewhere:

var user = builders.schema('user')();

Thanks! :-)

Al

unread,
Jan 17, 2014, 3:51:46 AM1/17/14
to tot...@googlegroups.com, rmo...@gmail.com
Thanks, one more question though, how could we separate modules into an independent dir? Thanks in advance.


On Wednesday, January 15, 2014 9:32:16 AM UTC-8, rmo...@gmail.com wrote:

Peter Širka

unread,
Jan 17, 2014, 4:19:07 AM1/17/14
to tot...@googlegroups.com, rmo...@gmail.com
Reply all
Reply to author
Forward
0 new messages