0
这显然是一个已知问题,但我不知道如何解决它在我的MEAN项目上。 似乎发送了多个请求。 我尝试了所有可找到的解决方案(旧的或更新的)。迄今为止失败。Mongojs发送后无法设置标题
res.end() res.redirect( '/')
似乎没有这样的伎俩。
这是我的代码,我处理我的HTTP方法
//delete
router.delete('/task/:id', function(req, res, next) {
let post = mongojs.ObjectId(req.params.id)
db.tasks.remove({_id: mongojs.ObjectId(req.params.id)}, function(err, task){
res.status(200).res.json(task)
return res.end()
console.log('SERVER - post ' + req.body.title + ' successfully deleted')
if (err) {
res.send('error message ' + err)
}
})
})
//update
router.put('/task/:id', function(req, res, next) {
var task = req.body
var updateTask = {}
updateTask.isDone = task.isDone
updateTask.title = task.title
if(!updateTask) {
res.status(404).send({
"error": "bad request"
})
} else {
db.tasks.update({_id: mongojs.ObjectId(req.params.id)}, updateTask, {}, function(err, task){
res.status(200).res.json(task)
return res.end()
if (err) {
res.send('error message ' + err)
}
})
}
})
这是一个角度的应用程序,所以这里是我的服务文件。虽然这与客户端没有关系
public deletePost(post: Post) {
console.log('SERVICE - post ' + post._id + ' successfully deleted')
return this.http.delete('http://localhost:1234/api/task/' + post._id)
.map(res => res.json)
}
public updatePost(postId : string, newText:string) {
}
public addPost(newPost: Post) {
var headers = new Headers();//??
headers.append('Content-Type', 'application/json');
console.log('successfully added (service)' + newPost.title)
let body = JSON.stringify({title: newPost.title, isDone:false});
console.log(body)
return this.http.post('http://localhost:1234/api/task', JSON.parse(body),{headers})
.map(res => res.json());
}
解决!我确实在客户端和服务器端发送了两次请求。 – SandyL
替换为:res.status(200).res.json(task)return res.end() – SandyL