Reputation: 1648
Hello guys i'm having troube about this one. I have 2 collection on mlab but they're the same database. So my problem is that my code is only saving on 1 collection . what might could be the problem here.
models/user.js
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
//User Schema
//authToken and isAuthenticated is for UserSchema of email-verification
var UserSchema = mongoose.Schema({
username:{
type: String,
unique: true,
index:true
},
password:{
type:String
},
email:{
type:String,
required: true
// unique: true
},
authToken:{
type: String,
required: true,
unique: true
},
IsAuthenticated:{
type: Boolean,
required: true
},
name:{
type:String
},
field:{
type:String
},
e_money:{
type:Number //this is the integer form in mongoose
}
});
//accesible variable from the outside
var User = module.exports = mongoose.model('User', UserSchema);
//create the user
module.exports.createUser= function(newUser, callback){
bcrypt.genSalt(10, function(err,salt){
bcrypt.hash(newUser.password, salt, function(err, hash){
//store hash in your password DB
newUser.password = hash;
newUser.save(callback);
});
});
}
models/infouser.js
var mongoose = require('mongoose');
//User Schema
var UserSchema = mongoose.Schema({
username:{
type: String,
unique: true,
index:true
},
name:{
type:String
},
field:{
type:String
},
e_money:{
type:Number //this is the integer form in mongoose
}
});
//accesible variable from the outside
var InfoUser = module.exports = mongoose.model('infouser', UserSchema);
//create the user
module.exports.createUser= function(newUser, callback){
newUser.save(callback);
}
And this is how i call them on my routes/users.js
var User = require('../models/user');
var InfoUser = require('../models/infouser');
var newUser = new User({
name: name,
email: email,
authToken: authToken,
IsAuthenticated: false,
username: username,
password: password,
field: field,
e_money: e_money //temporary emoney
});
var newUser2 = new InfoUser({
name: name,
username:username,
field:field,
e_money:e_money
});
InfoUser.createUser(newUser2,function(err,user){
if(err) throw err;
console.log(user);
});
//save the newly created user to database
User.createUser(newUser,function(err, user){
if(err) throw err;
console.log(user);
}
the two data saves on my users collection only . what i want here is that it the infouser data saves on my infouser collection and my users collection saves on my users collection
Upvotes: 0
Views: 3479
Reputation: 1648
I was doing something stupid . I was pushing something on heroku that is in different file so that's why my code wasn't working . Thanks everyone but i was actually doing it fine . the question above is the correct one . I'm sorry
Upvotes: 0
Reputation: 2133
mmmm that creteUser function on the model its really weird because you can pass the instance of any model there and it will call its own save function.
Why not instead just directly call the save function? (maybe you can eliminate scoping problems with that):
Something like (with promises):
var User = require('../models/user');
var InfoUser = require('../models/infouser');
var newUser = new User({
name: name,
email: email,
authToken: authToken,
IsAuthenticated: false,
username: username,
password: password,
field: field,
e_money: e_money //temporary emoney
});
var newUser2 = new InfoUser({
name: name,
username:username,
field:field,
e_money:e_money
});
newUser.save()
.then(function(user) {
console.log(user);
})
.catch(function(e) {
console.log(e);
});
newUser2.save()
.then(function(user) {
console.log(user);
})
.catch(function(e) {
console.log(e);
});
Upvotes: 2
Reputation: 1341
Add collection attribute in your schema.
var UserSchema = mongoose.Schema({...},{collection: 'collection_name'});
Upvotes: 0