CouchDB更新处理程序:文档ID不能为空 [英] CouchDB update handlers: document id must not be empty

查看:275
本文介绍了CouchDB更新处理程序:文档ID不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了简单的更新处理程序:

I wrote simple update handler:

 {
   "handler": "function (doc, req) { if (req.userCtx.roles.indexOf('editor') < 0) return [req.userCtx, 'access error']; if (!doc) { return [doc, 'nothing']; } doc.date = new Date(); doc.edited_by = req.userCtx.name; return [doc, toJSON(doc)]; }"
}

当我尝试从Mozilla HttpRequester http://192.168.0.34:5984/records/_design/records/_update/handler查询时,我得到

When I'm trying to query from Mozilla HttpRequester http://192.168.0.34:5984/records/_design/records/_update/handler, i'm getting

包含数据

{"title":"testtest","content":"test"}

并获得

{"error":"bad_request","reason":"Document id must not be empty"}

我已将_id添加到JSON

I've added _id to JSON

{"_id":"testid","title":"testtest","content":"test"}

没有运气.

更新:并且在浏览器中运行该查询将返回nothing,这可能表明没有发送文档,但是事实并非如此.我在做什么错了?

Update: and running that query in browser returns nothing, which may indicate that there's no document sent, but that's not true. What i'm doing wrong?

推荐答案

在CouchDB更新处理程序_id中,从未自动设置过,因此必须手动进行.最简单的解决方案是使用uuids,可从req.uuid获得.

In CouchDB update handler _id never been set automatically, so you must do it manually. The simplest solution is using uuids, which available at req.uuid.

关于更新处理程序的一件重要事情.它们的语法有些奇怪:您不能获取数据并返回doc,不会.您的更新处理程序带有docreq对象,其中第一个是更改文档(使用!doc检查文档是否存在,如果返回true-您可以创建新对象),第二个是请求数据,包括访问信息,uuid和身体.我们从服务器获取的数据不是在req中,而是在req.body中,这是原始字符串.必须首先使用JSON.parse(req.body)进行解析.经过一些检查后,我们可以返回-但是返回也不容易.如果我们有文档(如果没有的话,我们将对其进行更改),那么我们将在创建新对象.返回必须由数组[data_to_be_written, return_data]完成,其中第一项是数据库的文档,第二项是字符串(并且只有字符串,请使用toJSON(object)返回对象).

One important thing about update handlers. Their syntax someplace strange: you can't take data and return doc, no. Your update handler taking doc and req objects, where first is a changing document (check document existence using !doc, if it returns true — you can create new) and the second is request data, including access info, uuid and body. We're getting data from server not in req, but in req.body, which is raw string. Is must be parsed first using JSON.parse(req.body). After doing some checks, we can return — but return also not easy. If we have a doc — we're changing it, if not — we're creating new object. Return must be done by array [data_to_be_written, return_data], where first item is document for database and the second is a string (and only string, use toJSON(object) to return objects).

此外,还有我的更新处理程序函数示例,其中有很多肮脏的调试代码,这可能是错误的,但它可以在创建时正常工作(仍未选中更新).

In addition there's my update handler function example, there's a lot of dirty debug code and it can be wrong, but it working on creation (update still unchecked).

function (doc, req) {
    if (req['userCtx']['roles'].indexOf('editor') < 0) {
        return [null, 'Access denied'];
    }
    var rcv = JSON.parse(req.body);
    if (!doc) {
        if (!rcv['_id'] && !!rcv['title'] && !!rcv['content']) {
            return [{ '_id':req.uuid,'title': rcv['title'], 'content': rcv['content'], 'date': new Date(), 'edited_by': req['userCtx']['name'] }, 'Created' + toJSON(req)];
        }
        return [null, 'Empty' + toJSON({ 'no_id': !rcv['_id'], 'title': !!rcv['title'], 'content': !!rcv['content'], 'body':req.body })];
    }
    doc['date'] = new Date();
    doc['edited_by'] = req['userCtx']['name'];
    return [doc, 'Edited' + toJSON(doc)];
}


P.S>,请记住,即使更新处理程序也可以通过验证功能运行,因此,如果您的数据无法通过验证,则无论如何也不会写入数据.


P.S> and remember, that even update handler working over validation func, so if your data can't pass validation, it will not be written anyway.

这篇关于CouchDB更新处理程序:文档ID不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆