【问题标题】:case_clause error couchdb with cradlecase_clause 错误 couchdb with cradle
【发布时间】:2014-11-10 14:42:39
【问题描述】:

当我尝试将文档添加到我的 couchdb 时,我收到以下错误:

{case_clause, {[{<<"error">>, <<"invalid value">>}, {<<"reason">>, [<<"you need to give the ticket a ticketnumber">>]}]}}}

我的 validate_doc_update 方法如下所示:

function (newDoc, oldDoc, usrCtx) {
    var error = { reason: [], error: '' }
    switch (newDoc.type) {
        case 'application':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the application a name")
            }
            break;
        case 'client':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the client a name")
            }
            break;
        case 'department':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the department a name")
            }
            break;
        case 'release':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the release a name")
            }
            break;
        case 'ticket':
            if (!newDoc.ticketnumber) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a ticketnumber")
            }
            if (!newDoc.description) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a description")
            }
            if (newDoc.priority && !/\d*[,|.]\d*/.test(newDoc.priority)) {
                error.error = "invalid value"
                error.reason.push("the priority is invalid")
            }
            if (!newDoc.minutesperweek) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a impact in minutes per week")
            } else if (!/\d*[,|.]?\d*/.test(newDoc.minutesperweek)) {
                error.error = "invalid value"
                error.reason.push("the impact in minutes per week is invalid")
            }
            if (!newDoc.ordervolume) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a impact in orders per week")
            } else if (!/\d*/.test(newDoc.ordervolume)) {
                error.error = "invalid value"
                error.reason.push("the impact in orders per week is invalid")
            }
            break;
        case 'worker':
            if (!newDoc.firstname) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a firstname")
            }
            if (!newDoc.lastname) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a lastname")
            }
            if (!newDoc.emailaddress) {
                error.error = "invalid value"
                error.reason.push("\r\nyou need to give the worker an emailaddress")
            } else if (!/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(newDoc.emailaddress)) {
                error.error = "invalid value"
                error.reason.push("the emailaddress is invalid")
            }
            if (!newDoc.phonenumber) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a phonenumber")
            } else if (!/\d/g.test(newDoc.phonenumber)) {
                error.error = "invalid value"
                error.reason.push("the phonenumber is invalid")
            }
            break;
    }
    if (error.error != '') {
        throw { 'error': error.error, 'reason': error.reason }
    }
}

我希望,我会得到这样的结果:

{ 'error': 'Invalid value', 'reason': 'you need to give the ticket a ticketnumber' }

但是我得到了我在开始时写的错误。我的问题是什么?

如果重要的话,我会使用 cradle 和 node.js。

【问题讨论】:

    标签: node.js couchdb cradle


    【解决方案1】:

    validate_doc_update 函数只会抛出禁止或未经授权的错误(分别针对 403 和 401 HTTP 响应)。试试:

    throw({forbidden: { 'error': error.error, 'reason': error.reason }});
    

    【讨论】:

    • 根据this 它应该可以工作。我有什么选择?
    • 此示例无效并导致相同的行为。您可以自行查看。
    • 好的,但是,我该如何处理沙发网站上的验证呢?
    • 和你计划的一样。使用给定的修复 CouchDB 将响应您 HTTP 403 Forbidden 并在响应正文中包含以下 JSON:{"error":"forbidden","reason":{"error":"invalid value","reason":"you need to give the ticket a ticketnumber"}} - 对象结构看起来很奇怪,但我认为您会弄清楚如何改进它(;所以在现场进行验证) d 计划,只需从“原因”顶级字段中选择错误/原因。
    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多