当键不满足 couchbase 视图中的确切值时响应自定义错误

Response custom error when key not satisfies the exact value in couchbase view

我有一个像这样的文档-

{
 "fullUserName": "xxyz",
 "userFirstName": "xx",
 "userLastName": "xx",
 "primaryRole": "xy",
 "actualRole": "rrr",
 "userId": "abcd1234",
 "password":"c28f5c7cb675d41c7763ab0c42d",
 "type":"login",
 "channels":"*"
}

并查看 -

function (doc, meta) {
    if(doc.userId,doc.password,doc.type){
      emit([doc.userId,doc.password,doc.type],doc);
    }
}

当密钥与文档的 属性 匹配时它 return 文档否则它 return 空 JSON 就像 -

{"total_rows":2,"rows":[
  ]
}

现在我想在密钥不匹配时以JSON格式响应错误消息,例如-

{
  "Error-Code":"400",
  "Error-Msg":"user id and password does not match"
}

有什么办法吗,如果我走错了方向,请指正。 提前致谢。

您不应直接向用户公开视图查询结果,而应对其进行解释。

所以发出一个查看请求,查看响应并在那里执行检查的业务逻辑。例如:

"if result is empty it can only be because the user is unknown or the password hash didn't match the user, so return a business-specific error message, otherwise carry on with login"

您无法更改服务器的行为和响应格式,而且这样做也没有多大意义。这是您与服务器交互方式的 API 和契约。您应该在两者之间的层中添加自己的业务逻辑。