i'm developing an app with sails.js beta and mongodb.
I've two models in a many-to-many association, i can successfully associate and populate instances of these models using .add() and .populate() methods. My problem is now that the .remove() method seems to do nothing.
here the models:
//Menu.js
module.exports = {
schema : true,
attributes: {
name: {
type: 'string',
minLength: 3,
required: true
},
dishes: {
collection: 'dish',
via: 'menus',
dominant: true
}
}
};
//Dish.js
module.exports = {
schema : true,
attributes: {
name: {
type: 'string',
minLength: 3,
required: true
},
description: 'string',
menus: {
collection: 'menu',
via: 'dishes'
}
}
};
And here the controller actions...
addDishToMenu: function(req,res,next){
Menu.findOne(req.param('menu')).populate('dishes').exec(function(err,bean){
if(err) return next(err);
if(!bean) return next();
bean.dishes.add(req.param('dish'));
bean.save(function(err) {
if(err) return next(err);
res.redirect('/main/dishes/');
})
})
},
removeDishFromMenu: function(req,res,next){
Menu.findOne(req.param('menu')).populate('dishes').exec(function(err,bean){
if(err) return next(err);
if(!bean) return next();
bean.dishes.remove(req.param('dish'));
bean.save(function(err) {
if(err) return next(err);
res.redirect('/main/menu/' + req.param('menu'));
})
})
}
I can't figure out what i'm doing wrong. Any ideas?
This issue has been fixed and I confirmed it with the repo I sent earlier. If you update your sails, waterline, and sails-mongo versions you should be good to go.
Related
I have two controllers/models in my Sails project which is Clubs and Members. One club can have many members.
I try to put the id of 'Clubs' as a reference id (like a foreign key) in 'Members', so that I can retrieve the members of a club by using the reference id in 'Members'. I want to display the members according to their clubs at the homepage. However I could not find a way to pass the id value of 'Clubs' to the 'Members' controller. Below are some of the codes:
Clubs.js
module.exports = {
attributes: {
clubName: {
type: 'string',
},
clubDesc: {
type: 'string',
},
},
};
Members.js
module.exports = {
attributes: {
memberName: {
type: 'string',
},
clubId: {
type: 'string',
},
},
};
ClubsController.js
module.exports = {
list: function(req, res) {
Clubs.find({}).exec(function(err, club) {
if(err) {
res.send(500, {error: 'Database Error'});
}
res.view('pages/club-list', {clubs:club});
});
},
add: function(req, res) {
res.view('pages/club-add');
},
create: function(req, res) {
var clubName = req.body.clubName;
var clubDesc = req.body.clubDesc;
Clubs.create({clubName:clubName, clubDesc:clubDesc}).exec(function(err){
if(err) {
res.send(500, {error: 'Database Error'});
}
res.redirect('/clubs/list');
});
},
};
MembersController.js
module.exports = {
list: function(req, res) {
Members.find({}).exec(function(err, member) {
if(err) {
res.send(500, {error: 'Database Error'});
}
res.view('pages/member-list', {members:member});
});
},
add: function(req, res) {
res.view('pages/member-add');
},
create: function(req, res) {
var memberName = req.body.memberName;
var clubId = req.body.clubId;
Members.create({memberName:memberName,
clubId:clubId}).exec(function(err){
if(err) {
res.send(500, {error: 'Database Error'});
}
res.redirect('/members/list');
});
},
};
routes.js
module.exports.routes = {
'/': {
view: 'pages/homepage',
},
'/clubs/list': {
view: 'pages/club-list',
controller: 'Clubs',
action: 'list'
},
'/clubs/add': {
view: 'pages/club-add',
controller: 'Clubs',
action: 'add'
},
'/clubs/create': {
controller: 'Clubs',
action: 'create',
},
'/members/list': {
view: 'pages/member-list',
controller: 'Members',
action: 'list'
},
'/members/add': {
view: 'pages/member-add',
controller: 'Members',
action: 'add'
},
'/members/create': {
controller: 'Members',
action: 'create',
},
};
I'm really new to Sails.js here and I find that it's quite difficult to get resources on this matter. I'm not sure if I put this in a way that you guys could understand. But do ask for more details if you guys need more understanding. Thank you in advance.
If I understand correctly, you're looking to create a one-to-many association between Clubs and Members. Here's how it should look in Clubs.js, your 'many':
attributes: {
...
members: {
collection: 'Members',
via: 'club'
}
}
Then in Members.js, your 'many':
attributes: {
...
club: {
model: 'Clubs'
}
}
When you do Club.find(), the members key will be an array of member ids. If you do Club.find().populate('member'), the members key will be an array of fully-populated member objects.
Here are the docs on associations.
This isn't directly related to your question, buy since you are new to Sails, I am including a comment that will give you some advice on how to best use the framework. I hope it goes well!
I'm trying to create a view show in Sails. This is my code:
api / models / User.js:
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
lastname: {
type: 'string',
required: true
},
username: {
type: 'string',
required: true,
unique: true
}
}
};
api / controllers / UserController
Controller
module.exports = {
new:function (req, res) {
console.log('entre al formulario');
res.view();
},
create:function(req, res){
var userObj={
name : req.param('name'),
lastname : req.param('lastname'),
username : req.param('username')
}
User.create(userObj, function(err, user){
if(err){
console.log("Se encontro un error");
return res.redirect('/');
}
res.redirect('/user');
console.log("correcto");
});
},
show: function(req, res, next){
User.findOne(req.param('id'), function userFounded(err, user){
if(err)
console.log(err);
return next(err);
res.view({
user: user
});
});
}
};
And this is my view:
Show.ejs
When I go to localhost:1337/user/show/ this error comes out:
Error showing in console
However, I can insert, but I can't view the elements in the view show.ejs. Does someone know how to solve this?
Thank you in advance for help.
If you created your page with sails generate page *name* then you should have a name.page.js . There you have an data() {} section. In this section you have to define your user like user: {}
I am new to sails js, and I want to insert data into mongodb using many to many association , but I don't know which collection should I insert first and how to insert it. Below is my model:
// User.js
module.exports={
attributes:{
firstname:{
type:'string'
},
lastname:{
type:'string'
},
pets:{
collection:'pet',
via:'owners',
dominant: true
}
}
};
// Pet.js
module.exports={
attributes:{
name:{
type:'string'
},
type:{
type:'string'
},
owners:{
collection:'user',
via:'pets'
}
}
};
I tried to insert data into pet collection first , but I didn't see the collection is updated. This is how I insert data into pet collection:
Pet.create({'name':req.body.name,'type':req.body.type}).exec(function(err,data){
if(err) return next(err);
res.json(data);
});
Thank in advance for the help
If you have existing User records, you can specify an array of them when adding your pet:
Pet.create({
'name':req.body.name,
'type':req.body.type,
'owners': [userId1, userId2]
}).exec(function(err,data){
if(err) { return next(err); ]
res.json(data);
});
Or, you can specify the new Pet's ID when adding a new User:
Pet.create({
'name':req.body.name,
'type':req.body.type
}).exec(function(err,newPet){
if(err) { return next(err); ]
User.create({ name: 'tom', pets: [newPet.id] }).exec(function(err, newUser) {
if (err) { return next(err); }
return res.json({ pet: newPet, user: newUser });
});
});
After that, in Sails 0.12.x, you can add/remove items from the collection using the methods documented in the associations docs. In Sails 1.0, you'd use the addToCollection, removeFromCollection and replaceCollection methods.
i am trying to make a game. I need tu create a Match. I think the problem on this Way. The User create a Match. In a third table I save playerId and gameId. When another user join the match, I save again, playerId and gameId. Then, I make a query with player with gameId in common, and start the game.
first, One User may have many Games. second, One Match may have many Games. this is the Match model:
module.exports = {
attributes: {
name: {
type: 'string'
},
description: {
type: 'string'
},
game: {
collection: 'game',
via: 'gameId',
}
}
};
This is the User model:
var bcrypt = require('bcrypt');
module.exports = {
attributes: {
name: {
type:'string'
},
email: {
type: 'email',
required: true,
unique: true
},
password: {
type: 'string',
},
passwordConfirmation: {
type: 'string'
},
passwordEncrypted: {
type: 'string'
},
creator: {
collection: 'game',
via: 'playerId'
},
toJSON: function(){
var obj = this.toObject();
delete obj.password;
delete obj.passwordConfirmation;
delete obj._csrf;
return obj;
}
}, beforeCreate: function(values, next){
console.log("Acabo de entrar a eforeCreate");
var password = values.password;
var passwordConfirmation = values.passwordConfirmation;
if(!password || !passwordConfirmation || password != values.passwordConfirmation) {
var passwordDoesNotMatchError = [{
name: 'passwordDoesNotMatchError',
message: 'Las contraseñas deben coincidir'
}]
return next({
err: passwordDoesNotMatchError
});
}
require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, EncryptedPassword){
values.EncryptedPassword = EncryptedPassword;
next();
});
}
};
This is the Game model:
module.exports = {
attributes: {
gameId: {
model: 'match'
},
playerId: {
model: 'user'
}
}
};
finally, this is my controller:
module.exports = {
createMatch: function(req,res){
var matchObj = {
name: req.param('name'),
description: req.param('description'),
}
Match.create(matchObj, function(err, match){
if(err){
console.log("el error fue: " + err);
return res.send(err);
} console.log("Entro en create");
return res.json(match);
})
var gameObj = {
gameId: 'aclaration: I dont know how do I get the match.id',
playerId: req.session.me
}
Game.create(gameObj,function(err,game){
console.log("entro a GameCreate");
if(err){
return res.send(err);
} return res.json(game);
})
}
};
I can create the Match, but Game.create send this error:
_http_outgoing.js:344 throw new Error('Can\'t set headers after they are sent.'); ^
Error: Can't set headers after they are sent.
Somebody can help me? probably, I have many errors. Thanks.
Couple of things here:
Having an explicit Game model is not required in Sails. It can manage it implicitly, unless you want to store more information than just gameId and userId. So, you can just do away with Game model.
Please refer for async programming: How do I return the response from an asynchronous call?
Below code should work for you. Hope it helps.
module.exports = {
createMatch: function(req, res) {
var matchObj = {
name: req.param('name'),
description: req.param('description'),
};
Match.create(matchObj, function(err, match) {
if (err) {
console.log("el error fue: " + err);
return res.send(err);
}
console.log("Entro en create");
var gameObj = {
gameId: match.id,
playerId: req.session.me
};
Game.create(gameObj, function(err, game) {
console.log("entro a GameCreate");
if (err) {
return res.send(err);
}
return res.json(game);
// return res.json(match);
});
});
}
};
After testing model creation, I noticed that lifecycle callbacks were not getting called and upon reading Waterline's documentation I found:
NOTE: When using custom adapter methods the features of Waterline are not used. You no longer get the Lifecycle Callbacks and Validations as you would when using a defined Waterline method.
Though, I haven't knowingly used a custom adapter method, and that is the only reference I could find in the documentation about lifecycle callbacks getting disabled.
What criteria/setting of whichever files in config/* should I have to absolutely ensure that lifecycle callbacks are not disabled?
Here is a copy of my model for which the only lifecycle callback I use does not get called:
/**
* User.js
*
*/
var bcrypt = require('bcrypt');
module.exports = {
attributes: {
'email': {
type: 'email',
required: true,
unique: true
},
'username': {
type: 'string',
required: true,
unique: true,
minLength: 5,
maxLength: 16
},
'password': {
type: 'string',
required: true
},
'family': {
model: 'family'
},
'lastlogin': {
type: 'datetime',
defaultsTo: function() {return new Date().toISOString();}
},
beforeCreate: function(obj, cb) {
console.log("In beforeCreate");
bcrypt.hash(obj.password, 10, function(err, hash) {
if (err) {
console.log(err);
return cb(err);
}
obj.password = hash;
cb();
});
}
}
};`
Your callback need to be on the exports object, its not an attribute.
/**
* User.js
*
*/
var bcrypt = require('bcrypt');
module.exports = {
attributes: {
'email': {
type: 'email',
required: true,
unique: true
},
'username': {
type: 'string',
required: true,
unique: true,
minLength: 5,
maxLength: 16
},
'password': {
type: 'string',
required: true
},
'family': {
model: 'family'
},
'lastlogin': {
type: 'datetime',
defaultsTo: function() {return new Date().toISOString();}
},
},
beforeCreate: function(obj, cb) {
console.log("In beforeCreate");
bcrypt.hash(obj.password, 10, function(err, hash) {
if (err) {
console.log(err);
return cb(err);
}
obj.password = hash;
cb();
});
}
};