简体   繁体   English

更新从 MyCouch Extension 调用的函数

[英]Update functions called from MyCouch Extension

I'm trying to call update functions from MyCouch.我正在尝试从 MyCouch 调用更新函数。 The documentation of Mycouch reports only an outdated example with Post method on _design documents. Mycouch 的文档仅报告了_design文档上使用 Post 方法的过时示例。 But how to consume an Update function但是如何使用更新 function

{
  "_id": "_design/artists",
  "language": "javascript",
  "views": {
    "albums": {
        "map": "function(doc) {  if(doc.$doctype !== 'artist') return;  emit(doc.name, doc.albums);}" 
    }
  }
};

client.Documents.Post(designDocumentAsJson);

How to execute the update function on _design couchDB pushing a new document?如何在 _design couchDB 上执行更新 function 推送新文档?

The couchDB documentation tells about this call couchDB 文档讲述了这个调用

PUT /{db}/_design/{ddoc}/_update/{func}/{docid}

PUT /{db}/_design/{ddoc}/_update/{func}/{docid}放置 /{db}/_design/{ddoc}/_update/{func}/{docid}

Executes update function on server side for the specified document.
Parameters: 

    db – Database name
    ddoc – Design document name
    func – Update function name
    docid – Document ID

You can insert Design document like below to db: https://docs.couchdb.org/en/stable/api/ddoc/render.html#db-design-design-doc-update-update-name您可以将如下设计文档插入数据库: https://docs.couchdb.org/en/stable/api/ddoc/render.html#db-design-design-doc-update-update-name

{
  "_id": "_design/albums",
  "language": "javascript",
  "updates": {
    "addAlbum": "function(doc, req) {\n    if (!doc){\n      return [null, {'code': 400,\n                     'json': {'error': 'missed',\n                              'reason': 'no document to update'}}]\n    } else {\n        var body = JSON.parse(req.body);\n        doc.albums.push(body.album);\n        return [doc, {'json': {'status': 'ok'}}];\n    }\n}\n"
  }
}

function (Without stringifying): function(无串化):

function(doc, req) {
    if (!doc){
      return [null, {'code': 400,
                     'json': {'error': 'missed',
                              'reason': 'no document to update'}}]
    } else {
        var body = JSON.parse(req.body);
        doc.albums.push(body.album);
        return [doc, {'json': {'status': 'ok'}}];
    }
}

Example doc:示例文档:

{
  "_id": "albumsId1",
  "albums": [
    1
  ]
}

After Api Api之后

Request:要求:

POST http://localhost:5984/test/_design/albums/_update/addAlbum/albumsId1
Content-Type:application/json
Accept:application/json

{"album":2}

Response:回复:

{
    "status": "ok"
}

doc after updating更新后的文档

{
  "_id": "albumsId1",
  "_rev": "19-7edb16db3bae388685f554138d562bd0",
  "albums": [
    1,
    2
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM