CouchDB update in-place


#1

Hi,
I am trying to update a CouchDB document from my Ionic 3 app. I need to add an array field called “related_ids” (which may or may not already exist in that document) and if it does already exist, I need to push another value to it. eg

"related_ids": [
    "foo",
    "bar",
    etc
 ],

I figured this should be achievable with an Update Handler in a Couch design_doc (though Im open to alternative routes, as I havent managed to get it working!) From ionic, I run this in my provider:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import 'rxjs/add/operator/map';

apiUrl = "http://localhost:5984/my-users/_design/utils/_update/in-place-query/";

constructor(public http: HttpClient) {
  }

updateUser(sender_id, currentUser) {
return new Promise((resolve, reject) => {
      
      this.http.put(this.apiUrl + sender_id + '?field=related_ids&value=newuser','null')
     .subscribe(res => {
         console.log(res);
         resolve(res);
      }, (err) => {
         console.log(err);
      });
   });
}

The updateUser function above receives the correct data, and connects to the Couch design doc below:

{
"_id": “_design/utils”,
"_rev": “26-6ed7cb5d2b567d6ffbfa743c7c4a06dd”,
“updates”: {
“in-place-query”: “function (doc, req) { var field = req.query.field; var value = req.query.value; doc[field] = value; return [doc]; }”
}
}

…but doesnt write the document and returns with this error:

error: "render_error", reason: "undefined response from update function"}
error: "render_error"
reason: "undefined response from update function"
Http failure response 500 Internal Server Error

I have tried sending with params instead of vars in the url, but same deal.
Once I get the design doc actually updating the document I will have to create an array rather than the simple name/value pair that it is trying to create at the moment. I figured it would have been a trivial task to get that far, but Ive been trying combinations of this for 2 days now :frowning:
Can someone please point out something that is obviously wrong with my code! Or maybe suggest an alternative route to update a CouchDB document from within Ionic.

Thanks in advance


#2

okay, so it turns out it was the design document that was awry. I now have single values updating. I now need to get any supplied required_ids value to be added to the required_ids array within the user’s document. My design document is as follows (but doesnt work)


function (doc, req) {
  if (doc.related_ids) {
    var jsonStr = doc.related_ids;
    var obj = JSON.parse(jsonStr);
    obj['related_ids'].push(req.query.related_ids);
    jsonStr = JSON.stringify(obj);
    return [doc, req];
  } else {
    doc.related_ids = req.query.related_ids;
    return [doc, req];
  }
}

Close! If related_ids doesnt exist it does create it. Just need to make the if exists bit work now…
Currently throws a syntax error -
function raised error: (new SyntaxError(\"JSON.parse\",


#3

sorted! Ended up being a simple case of constructing an array, iterating through any existing objects then adding them to the array, before pushing the new one in.
Inline CouchDB handlers for the win.