Answer the question
In order to leave comments, you need to log in
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'
}
});
}
]);
, 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){
});
};
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);
exports.addcomments = function(req,res, next){
console.log('server addcomments');
};
exports.listall = function(req,res,next){
console.log('sever lisall');
};
GET http://localhost:3000/server-error 500 (Internal Server Error)
Answer the question
In order to leave comments, you need to log in
most likely the server redirected you to server-error, and the redirect request was already GET
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.
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'
}
};
$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;
});
};
discuss.addcomment
, including with $ TypeError: undefined is not a function
at Scope.$scope.addComment
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question