S
S
Sergey2015-04-12 17:02:30
Angular
Sergey, 2015-04-12 17:02:30

Can you help me figure out $resource and server response?

I make comments on articles (Two related schemas DiscussSchema and CommentSchema).
Made a service

angular.module('discusses').factory('Comments', ['$resource',
  function($resource) {
    return $resource('api/discusses/comments', 
    {
      update: {
        method: 'POST'
      }
    });
  }
]);
,
In the angular controller
var CommentResource = Comments;
$scope.addComment = function(){
    var commentRes = new CommentResource();
    console.log(commentRes);
    commentRes.discuss_id = $stateParams.discussId;
    commentRes.body = $scope.commentData.body;
    commentRes.user = user;

    commentRes.$save(function(result){
      
    });
};

Everything is displayed in the console normally. id, user as object, all is well.
In server parties I write
module.exports = function (app) {
    var disсusses = require('../controllers/disсusses.server.controller');
    var disсussesPolicy = require('../policies/disсusses.server.policy');
app.route('/api/disсusses/comments').all(disсussesPolicy.isAllowed)
            .post(disсusses.addcomments)
            .get(disсusses.listall);

in the server controller I write
exports.addcomments = function(req,res, next){
    console.log('server addcomments');
  };
exports.listall = function(req,res,next){
    console.log('sever lisall');
};

But when I click addComment() I get
GET http://localhost:3000/server-error 500 (Internal Server Error)

Those. something is sent there. And the question is why GET? $save sort of goes like POST
What's going on and who's to blame?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-04-12
Protko @Fesor

most likely the server redirected you to server-error, and the redirect request was already GET

T
Timofey, 2015-04-13
@mr_T

If there was a redirect (and it almost certainly was), then you can see in the developer panel all the requests that the browser sent, in particular, the request that received the redirect command in response.
And so - 500 error is sent manually somewhere in the application, most likely, so you need to first find out where exactly. Most likely some middleware like bodyparser failed to process the request. In general, it is strange that there was a redirect to /server-error. In a good way, an error should be generated not on a redirect, but directly on the page on which it occurred. Well, take a look at the application console, there may also be something useful there.

S
Sergey, 2015-04-14
@TsarS

Then the next question. Now I make embedded comments.
Angular :

angular.module('discusses').factory('Discusses', ['$resource',
  function($resource) {
    return $resource('api/discusses/:discussId', { discussId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    },
       {
       	addcomment: {
       		method: 'PUT'
       	}
    };

In the controller
$scope.addComment = function() {
      var discuss = new Discusses();
      var content = $scope.content;

      discuss.addcomment(function() {
        discuss.comments.push(content);
        $location.path('discusses/' + discuss._id);
    
      }, function(errorResponse) {
        $scope.error = errorResponse.data.message;
      });
    };

In case
discuss.addcomment, including with $
issues
TypeError: undefined is not a function
    at Scope.$scope.addComment

If I change it to $update, then everything works (well, in fact, it turns out crap, but the request gets to the api server)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question