MEAN Stack:如何将函数的结果更新到数据库?

我有一个评论系统,我需要将upvote存储在数据库中,每当upvote图标被点击。 function我增加了数量,但一旦我刷新它再次说0。 如何将其直接存储到数据库? 这里是代码:

public / javascripts目录中的ang.js中:

var app=angular.module('peopleComments',['ui.router']); app.factory('comments',['$http', function($http){ var c={ comments:[] }; //loading all existing comments with getAll() c.getAll=function(){ return $http.get('/comments').success(function(data){ angular.copy(data, c.comments); }); }; //function which creates the new comments for updating in the database c.create = function(comment) { return $http.post('/comments', comment).success(function(data){ c.comments.push(data); });}; return c; }]); app.controller('Base',[ '$scope','comments',function($scope,comments){ $scope.comments=comments.comments; $scope.addComment=function(){ if(!$scope.username||$scope.username==''){$scope.username='Anonymous';} if(!$scope.contents||$scope.contents==''){return;} comments.create({ username: $scope.username, contents: $scope.contents, }); $scope.username=''; $scope.contents=''; } $scope.increaseUpvotes=function(comment){ //function which updates the upvotes comment.upvotes+=1; } }]); 

您需要在服务中创build一个c.update(comment)方法,该方法需要注释来更新,然后在您的API中调用相应的$http.put()方法以在单击upvotebutton时更新评论。

更新 :另外一个潜在的问题 – 如果你没有专门创build一个comment.upvotes属性,并将其默认设置为0,那么你的comment.upvotes += 1可能会添加一个nullundefined而不是真正添加一个。