angularjs - 我可以有两个 .post 请求解析到同一个 api 端点吗?
angularjs - can I have two .post requests resolving to same api endpoint?
如果这是一个愚蠢的问题,我深表歉意,请允许我稍微解释一下。我正在 运行ning 一个 MEAN 应用程序。
在我的 CRUD 生成模块的服务器路由中,我包含 两个单独的 post 请求 到相同的 api 端点。
app.route('/api/tasks/:taskId').all(tasksPolicy.isAllowed)
.get(tasks.read)
.put(tasks.update)
.delete(tasks.delete)
.post(tasks.newCo)
.post(tasks.newOffer);
其中每一个都根据 taskId 向我的 mongo 任务文档执行单独的推送请求。
当我一次 运行 一个函数时,每个单独的函数都能成功运行并推送到正确的数组中。
但是,当我 运行 同时在同一页面上包含这两个函数时,newOffer 函数会将一个空值推送到 newCo 数组中。并且 newCo 功能继续成功运行。
我不知道为什么..
再次,如果这是一个愚蠢的问题,我深表歉意。
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
您不能对两个 post
方法使用相同的 api
。您可以对相同的 api
使用不同的方法,例如 (post, get, put, delete ...
) 但不能多次使用相同的方法。
你应该对两个 post
方法使用不同的 api
。
喜欢完成任务api
app.route('/api/tasks/:taskId').all(tasksPolicy.isAllowed)
.get(tasks.read)
.put(tasks.update)
.delete(tasks.delete)
.post(tasks.newCo);
要约 api
app.route('/api/offer/:taskId').all(tasksPolicy.isAllowed)
.post(tasks.newOffer);
如果您使用相同的 api
和两个 post
方法,那么总是调用第一个 post
方法,所以第二个总是 无法访问 。当您调用此 api
为该任务添加 offer 时,然后调用 tasks.newCo
函数,当您想要接收 req.body.comment
时,获取 null
或undefined
所以添加 empty
或 null
comment 但永远不会添加 offer.
如果这是一个愚蠢的问题,我深表歉意,请允许我稍微解释一下。我正在 运行ning 一个 MEAN 应用程序。
在我的 CRUD 生成模块的服务器路由中,我包含 两个单独的 post 请求 到相同的 api 端点。
app.route('/api/tasks/:taskId').all(tasksPolicy.isAllowed)
.get(tasks.read)
.put(tasks.update)
.delete(tasks.delete)
.post(tasks.newCo)
.post(tasks.newOffer);
其中每一个都根据 taskId 向我的 mongo 任务文档执行单独的推送请求。
当我一次 运行 一个函数时,每个单独的函数都能成功运行并推送到正确的数组中。 但是,当我 运行 同时在同一页面上包含这两个函数时,newOffer 函数会将一个空值推送到 newCo 数组中。并且 newCo 功能继续成功运行。
我不知道为什么..
再次,如果这是一个愚蠢的问题,我深表歉意。
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
您不能对两个 post
方法使用相同的 api
。您可以对相同的 api
使用不同的方法,例如 (post, get, put, delete ...
) 但不能多次使用相同的方法。
你应该对两个 post
方法使用不同的 api
。
喜欢完成任务api
app.route('/api/tasks/:taskId').all(tasksPolicy.isAllowed)
.get(tasks.read)
.put(tasks.update)
.delete(tasks.delete)
.post(tasks.newCo);
要约 api
app.route('/api/offer/:taskId').all(tasksPolicy.isAllowed)
.post(tasks.newOffer);
如果您使用相同的 api
和两个 post
方法,那么总是调用第一个 post
方法,所以第二个总是 无法访问 。当您调用此 api
为该任务添加 offer 时,然后调用 tasks.newCo
函数,当您想要接收 req.body.comment
时,获取 null
或undefined
所以添加 empty
或 null
comment 但永远不会添加 offer.