angularjs - 我有两个要求.post的解决,以相同的API端点? [英] angularjs - can I have two .post requests resolving to same api endpoint?
问题描述
我道歉,如果这是一个愚蠢的问题,请允许我解释一下。我运行一个应用均值。
在我的CRUD服务器途径产生模块我已经包括两个独立的POST请求以同样的API终点。
app.route('/ API /任务/:TASKID')。所有(tasksPolicy.isAllowed)
获得(tasks.read)
。把(任务。更新)
.delete(tasks.delete)
。员额(tasks.newCo)
。员额(tasks.newOffer);
每个这些执行单独的请求推到我的任务蒙戈文件的基础上,TASKID。
当我一次运行一个函数,每个函数的工作,并成功推入正确的阵列。
然而,当我运行包括在同一页上的功能都在同一时间newOffer函数把一个空值到新公司阵。而新公司的功能继续成功地运行。
我不知道为什么。
再次,我道歉,如果这是一个愚蠢的问题。
server.controller.js
/ **
*添加新评论
* /
exports.newCo =功能(REQ,RES){
Task.findOneAndUpdate({_ ID:req.params.taskId},
{
$推:{
评论:req.body.comment
}
},{
新://真正返回更新后的文档
})
.exec(功能(错误,任务){
如果(错误){
返回res.status(400)。发送({消息:'未能添加注释由于参数无效'});
}
返回res.status(200)。发送(任务);
});
};/ **
*添加新报价
* /
exports.newOffer =功能(REQ,RES){
Task.findOneAndUpdate({_ ID:req.params.taskId},
{
$推:{
提供:req.body.offer
}
},{
新://真正返回更新后的文档
})
.exec(功能(错误,任务){
如果(错误){
返回res.status(400)。发送({消息:'未能提供补充由于参数无效'});
}
返回res.status(200)。发送(任务);
});
};
client.controller.js
vm.newCo =功能(){
$ http.post('/ API /任务/+ task._id,{注释:{注释:vm.task.newComment,用户:vm.authentication.user,profileImageURL:vm.authentication.user.profileImageURL,显示名:VM .authentication.user.displayName}})
。然后(successCallback,errorCallback); 功能successCallback(RES){
$ state.transitionTo($ state.current,$ state.params,{
重载:真的,
继承:假的,
通知:真
});
} 功能errorCallback(RES){
vm.error = res.data.message;
}
};
//关闭新评论功能
//新报价
vm.newOffer =功能(){
$ http.post('/ API /任务/+ task._id,{报价:{offerDesc:vm.task.offerDesc,offerPrice:vm.task.offerPrice,用户:vm.authentication.user,profileImageURL:vm.authentication .user.profileImageURL,显示名:vm.authentication.user.displayName}})
。然后(successCallback,errorCallback);
警报('成功了!');
功能successCallback(RES){
$ state.transitionTo($ state.current,$ state.params,{
重载:真的,
继承:假的,
通知:真
});
} 功能errorCallback(RES){
vm.error = res.data.message;
}
};
//关闭新的报价功能
您不能使用相同的 API
两后
方法。您可以使用相同的 API不同的方法
像(后,GET,PUT,删除...
),但不一样的方法多的时间。
您应该使用不同的 API
两后
方法。
像任务 API
app.route('/ API /任务/:TASKID')。所有(tasksPolicy.isAllowed)
获得(tasks.read)
。把(任务。更新)
.delete(tasks.delete)
。员额(tasks.newCo);
有关报价 API
app.route('/ API /报价/:TASKID')。所有(tasksPolicy.isAllowed)
。员额(tasks.newOffer);
如果您使用相同的 API
和两个后
方法,那么总是先打电话发布
方法,因此第二个永远的无法访问即可。当你调用这个 API
添加报价作为该任务,然后调用 tasks.newCo
功能当你想要得到 req.body.comment
GET 空
或未定义
所以加空
或空
注释但绝不会增加报价
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
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.
这篇关于angularjs - 我有两个要求.post的解决,以相同的API端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!