angularjs - can I have two .post requests resolving to same api endpoint?

风流意气都作罢 提交于 2019-12-12 04:08:08

问题


I apologise if this is a stupid question, please allow me to explain a little. I am running a MEAN application.

In the server routes for my CRUD generated module I have included two separate post requests to the same api end point.

app.route('/api/tasks/:taskId').all(tasksPolicy.isAllowed)
.get(tasks.read)
.put(tasks.update)
.delete(tasks.delete)
.post(tasks.newCo)
.post(tasks.newOffer);

Each of these perform a separate push request into my mongo document for task, based on the taskId.

When I run one function at a time, each individual function works successfully and pushes into the correct array. However, when I run include both functions on the same page at the same time the newOffer function pushes a null value into the newCo array. And the newCo function continues to work successfully.

I have no idea why..

again, I apologise if this is a stupid question.

server.controller.js

/**
 * Add a new comment
 */
exports.newCo = function(req, res) {
  Task.findOneAndUpdate({_id: req.params.taskId}, 
    {
      "$push": {
        comments: req.body.comment
      }
    }, {
      new: true //to return updated document
    })
    .exec(function(error, task) {
      if (error) {
        return res.status(400).send({message: 'Failed to add comment due to invalid params!'});
      }
      return res.status(200).send(task);
    });
};

/**
 * Add a new offer
 */
exports.newOffer = function(req, res) {
  Task.findOneAndUpdate({_id: req.params.taskId}, 
    {
      "$push": {
        offers: req.body.offer
      }
    }, {
      new: true //to return updated document
    })
    .exec(function(error, task) {
      if (error) {
        return res.status(400).send({message: 'Failed to add offer due to invalid params!'});
      }
      return res.status(200).send(task);
    });
};

client.controller.js

vm.newCo = function() {
            $http.post('/api/tasks/' + task._id , {comment: { comment: vm.task.newComment, user: vm.authentication.user, profileImageURL: vm.authentication.user.profileImageURL, displayName: vm.authentication.user.displayName } })
            .then(successCallback, errorCallback); 

      function successCallback(res) {
        $state.transitionTo($state.current, $state.params, {
                    reload: true,
                    inherit: false,
                    notify: true
                });
      }

      function errorCallback(res) {
        vm.error = res.data.message;
      }
    };
        //close new comment function


    //new offer
        vm.newOffer = function() {
            $http.post('/api/tasks/' + task._id , {offer: { offerDesc: vm.task.offerDesc, offerPrice: vm.task.offerPrice, user: vm.authentication.user, profileImageURL: vm.authentication.user.profileImageURL, displayName: vm.authentication.user.displayName } })
            .then(successCallback, errorCallback); 
            alert('it worked!');
      function successCallback(res) {
        $state.transitionTo($state.current, $state.params, {
                    reload: true,
                    inherit: false,
                    notify: true
                });
      }

      function errorCallback(res) {
        vm.error = res.data.message;
      }
    };
         //close new offer function

回答1:


you couldn't use same api for two post method. you can use different methods for same api like (post, get, put, delete ...) but not same method multiple time.

you should use different api for two post method .

like for task api

app.route('/api/tasks/:taskId').all(tasksPolicy.isAllowed)
.get(tasks.read)
.put(tasks.update)
.delete(tasks.delete)
.post(tasks.newCo);

for offer api

app.route('/api/offer/:taskId').all(tasksPolicy.isAllowed)
.post(tasks.newOffer);

if you use same api and two post method then always call first post method so second one always unreachable. When you call this api to add offer for that task then call tasks.newCo function and when you want receive req.body.comment get null or undefined so add empty or null comment but never will add offer.



来源:https://stackoverflow.com/questions/36777769/angularjs-can-i-have-two-post-requests-resolving-to-same-api-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!