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
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