MEAN堆栈:如何更新功能的结果数据库? [英] MEAN Stack: How to update a function's result to the database?

查看:113
本文介绍了MEAN堆栈:如何更新功能的结果数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个评论系统,我需要的upvotes存储在数据库中,只要点击该图标给予好评。我具备的功能数量的增加,但只要我刷新它,它又说0。我如何直接存储到数据库?
这里是code:

ang.js 公共/ JavaScript的的目录:

  VAR应用= angular.module('peopleComments',['ui.router']);
app.factory('意见',['$ HTTP',函数($ HTTP){
变种C = {
注释:[]
};//装载与GETALL现有的所有评论()
c.getAll =功能(){
返回$ http.get('/意见)。成功(功能(数据){
    angular.copy(数据,c.comments);
});
};//这对于更新数据库中创建新的评论功能
c.create =功能(评论){
返回$ http.post('/评论,评论).success(功能(数据){
c.comments.push(数据);
});};
返回℃;
}]);app.controller('基地',[
'$范围','意见',函数($范围,评论){
  $ scope.comments = comments.comments;    $ scope.addComment =功能(){
        如果(!$ scope.username || $ scope.username ==''){$ scope.username ='无名氏';}
        如果{回报;}($ scope.contents || $ scope.contents ==''!)
        comments.create({
            用户名:$ scope.username,
            内容:$ scope.contents,
            });
        $ scope.username ='';
        $ scope.contents ='';
    }
$ scope.increaseUpvotes =功能(评论){//功能,更新upvotes
  comment.upvotes + = 1;
}}]);


解决方案

您需要创建您服务 c.update(评论)方法,它要评论更新,然后调用你的API更新给予好评按钮被点击时的注释在对应的 $ http.put()方法。

更新:还有一个潜在的问题 - 如果你不是专门创建一个 comment.upvotes 属性,默认情况下那么你将其设置为0 comment.upvotes + = 1 可能会被加一未定义并没有真正增加一个。

I'm having a commenting system and I need to store the upvotes in the database whenever the upvote icon is clicked. The function I have increases the number but as soon as I refresh it, it says 0 again. How do I store it directly to the database? Here is the code:

In the ang.js in the public/javascripts directory:

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;
}

}]);

解决方案

You need to create a c.update(comment) method in your service that takes a comment to update and then calls a corresponding $http.put() method in your API to update the comment when the upvote button is clicked.

Update: One more potential issue - if you are not specifically creating a comment.upvotes property and setting it to 0 by default then your comment.upvotes += 1 might be adding one to null or undefined and not really adding one.

这篇关于MEAN堆栈:如何更新功能的结果数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆