Skip to content

Instantly share code, notes, and snippets.

@andrewjmead
Created March 2, 2016 02:14
Show Gist options
  • Select an option

  • Save andrewjmead/f361ecb6042c48e62258 to your computer and use it in GitHub Desktop.

Select an option

Save andrewjmead/f361ecb6042c48e62258 to your computer and use it in GitHub Desktop.
Keith Composite Index Sequelize
var UserTodos = sequelize.define('UserTodos', {
favorite: {
type: Sequelize.BOOLEAN
},
UserId: {
type: Sequelize.INTEGER,
unique: 'compositeIndex'
},
TodoId: {
type: Sequelize.INTEGER,
unique: 'compositeIndex'
}
});
@keithmichelson

Copy link
Copy Markdown

original route
`app.post('/favorites/:id', middleware.requireAuthentication, function(req, res){
var favoriteId = parseInt(req.params.id, 10);
var body = {
favoriteId: favoriteId
};

db.favorite.create(body).then(
  function(favorite) {
    req.user.addFavorite(favorite).then(
      function() {
        return favorite.reload();
      }).then(
      function() {
        res.json(favorite.toJSON());
      });
  },
  function(e) {
    res.status(400).json(e);
  });

});`

This will fix the error adding another function to catch the error. But it gives me a database entry with the favorite id and a Null userId. I would think it should add anything.

`app.post('/favorites/:id', middleware.requireAuthentication, function(req, res){
var favoriteId = parseInt(req.params.id, 10);
var body = {
favoriteId: favoriteId
};

db.favorite.create(body).then(
  function(favorite) {
    req.user.addFavorite(favorite).then(
      function() {
        return favorite.reload();
      },
      function(e) {
        console.log(e);
      }).then(
      function() {
        res.json(favorite.toJSON());
      });
  },
  function(e) {
    res.status(400).json(e);
  });

});`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment