0
votes

I use couchDB 3.2.1. I have a design document that contains views and now an update function. I get 500 (Internal Server Error) when trying to run the the function, passing a document using PUT and the fetchAPI: http://my.company.xyz:5984/brunel/_design/brunel/_update/customerPrefs/5509

I tried debugging by just returning the doc at the end of the function, but still get the same issue.

The design document look like this:

"_id": "_design/brunel",
"_rev": "11-33c446a585aa4a63f3f848bd4979d721",
   "views": {
    "productCategoryNames": {
      "map": "function (doc) {  if ((doc.recordType === 'product') && doc.categoryName && doc.categoryCode){    emit([doc.categoryCode, doc.categoryName], null);  }}",
      "reduce": "_count"
    },
    "invoiceNumbers": {
      "map": "function (doc) {  if (doc.recordType === 'invoice'){    emit(doc.invoiceNumber, null);  }}",
      "reduce": "_count"
    },
    "supplierNames": {
      "map": "function (doc) {  if ((doc.recordType === 'supplier') && doc.supplierName){    emit(doc.supplierName, null);  }}",
      "reduce": "_count"
    },
    "productNames": {
      "map": "function (doc) {  if ((doc.recordType === 'product') && doc.description){    emit(doc.description, null);  }}",
      "reduce": "_count"
    },
    "customerNames": {
      "map": "function (doc) {  if ((doc.recordType === 'customer') && doc.customerName){    emit(doc.customerName, null);  }}",
      "reduce": "_count"
    },
    "batchReceiveDates": {
      "map": "function (doc) {  if (doc.recordType === 'batch'){    emit(doc.batchNumber, null);  }}"
    }
  },
  "updates": {
    "customerPrefs": "function (doc, req){  return [doc];  }"
  }
}

I think there may be something wrong with the way I declare my update function as part of the design document.

My aim is to use in place update functions to pass key pairs, that will be stored/appended in an array, that's part of an existing document.

The couchDB log shows the following error:

[error] 2019-06-23T19:11:48.879976Z [email protected] <0.6521.378> 6734daf161 OS Process Error <0.14007.353> :: {<<"render_error">>,<<"undefined response from update function">>}

1
From the docs, you're supposed to return a two-element array, which you are not doing. If you add a second element to your array, do things work? - Flimzy
Thank you. I've also learned some other hard lessons: - You cannot use comments in the functions. - You cannot use "let" for declaring variables. - Some ES6 notations doesn't seem to work. - TuinBoy

1 Answers

0
votes

Thank you to Flimzy for pointing out that the function must return two values. I also found it useful to look in the couchDB log file for debugging JavaScript errors in the update handler.