Monday, 3 April 2017

Getting upvotes to persist to database in MEAN stack app

I am trying to get the upvote on items to save to the database when a user clicks on the button in the html file. Currently, it increases the upvote number on the page, but will not save it to the database upon refreshing the page. Any suggestions would be greatly appreciated!

html file: <span class="glyphicon glyphicon-heart-empty" ng- click="increaseUpvotes(item)"> </span>

Item model in which users can "upvote" the item:

const ItemSchema = new mongoose.Schema({
title: String,
photo_url: String,
upvotes: {type: Number, default: 0},
maker: String,
description: String,
price: Number
})

in my index.js:

app.put('/api/items', function(req, res, next){
req.item.upvote(function(err, item){
if(err){return next(err)}
res.json(item)
})
})

and in my controller:

.factory("ItemFactory", [
"$resource",
ItemFactoryFunction
])

function ItemFactoryFunction( $resource, $http){
return $resource("/api/items/:title", {}, {
update: {method: "PUT"}
})
return $resource("/api/items/:title/upvote", {}, {
update: {method: "PUT"}
})

function ItemIndexControllerFunction(ItemFactory, $state, $scope){
this.items = ItemFactory.query()
$scope.items = ItemFactory.items
$scope.increaseUpvotes = function(item){
item.upvotes +=1
}
}



via hs1919

No comments:

Post a Comment