Javascript 如何处理angularjs中的服务器端错误
如何使用angularjs处理服务器端错误。我看到了一些解决方案,比如,但它们并不真正符合我的需要/也许我需要改变我失败的行为。注意,我在服务器端处理路由 我的问题是,在服务器端,我运行了一些Javascript 如何处理angularjs中的服务器端错误,javascript,angularjs,Javascript,Angularjs,如何使用angularjs处理服务器端错误。我看到了一些解决方案,比如,但它们并不真正符合我的需要/也许我需要改变我失败的行为。注意,我在服务器端处理路由 我的问题是,在服务器端,我运行了一些mongo查询,然后返回到我的路由。请注意,这些查询也可能生成自定义错误(例如字段的长度不合适)。例如: 服务器端 productServices.getIngredient() .success(function(result) { //error should
mongo
查询,然后返回到我的路由。请注意,这些查询也可能生成自定义错误(例如字段的长度不合适)。例如:
服务器端
productServices.getIngredient()
.success(function(result) {
//error should be handled here e.g. result.error === 1
$scope.ingredient = result.data
})
.error(function(err) {
console.log('error ' + err)
})
IngCreditController
controller.getIngredient = function(cb) {
Ingredient.findOne({},cb)
}
在我的路由处理程序中,我调用此函数:
function(req, res) {
ingredientController.getIngredient(function(err, data) {
if (err)
res.json({
error: 1
})
else
res.json({
error: 0,
data: 'success'
})
})
}
请注意,json中也可能包含错误消息
客户端
productServices.getIngredient()
.success(function(result) {
//error should be handled here e.g. result.error === 1
$scope.ingredient = result.data
})
.error(function(err) {
console.log('error ' + err)
})
我做得对吗?如果是,那么我应该如何向用户提供一些反馈。一个解决方案是创建一个显示错误消息的服务和指令。在主模板中加载指令,然后使用服务更新它 另一种选择是使用库:
因此,我需要在每个
ajax
请求的success
中使用error
参数调用此服务的getDatabaseResult
?你能不能更新你的结果,使它不是一般的——你可以保留一般形式——但它适用于我的确切问题?@Pio你可以称咆哮的addsuccessage或addErrorMessage