User-Defined Functions and Eventing

I’m trying to invoke an N1QL UPDATE statement inside of an eventing function. The N1QL query works fine when invoked outside of the eventing procedure, but I am not able to deploy the eventing function because of a “SyntaxError: Unexpected template string”.

The issue seems to be, that the statement calls a user-defined function.
Aren’t user-defined functions supported in eventing N1QL statements?
My UPDATE statement looks like the following:

UPDATE t SET a[i].p = {"id": $meta.id, "rev": $meta.seq} 
FOR i: s IN a WHEN s.b = $b END 
WHERE ANY aa IN a SATISFIES aa.b = $b AND userDefinedFunction($meta.id) >= userDefinedFunction(aa.p.id) END ;

Hi @Firefox2005

Without running the query I do see you are attempting to reference the passed in “meta” value.

As per the document Language Constructs | Couchbase Docs

You must use $<variable> , as per N1QL specification, to use a JavaScript variable in the query statement. The object expressions for substitution are not supported and therefore you cannot use the meta.id expression in the query statement.

Instead of meta.id expression, you can use var id = meta.id in an N1QL query.

I think a small change as follows will fix your issue:

// You need to define local JavaScript variable to use in inline N1QL
var id = meta.id;
var seq = meta.seq;

UPDATE t SET a[i].p = {"id": $id, "rev": $seq} 
FOR i: s IN a WHEN s.b = $b END 
WHERE ANY aa IN a SATISFIES aa.b = $b AND userDefinedFunction($id) >= userDefinedFunction(aa.p.id) END ;

Best

Jon Strabala

Hi @jon.strabala

thanks for the hint. But unfortunately that doesn’t fix it.
As soon as I remove the userDefinedFunction() call I am able to deploy it, even when using the local $id variable.

@Firefox2005,

Sorry your still having issues (I didn’t see the UDF like I said I didn’t look too closely -nor did I read the subject carefully)

  • Supply a JSON document for use in your Query
  • Identify the couchbase-server version so I am working in the same environment.
  • Supply your UDF

and I will get to the bottom of your problem, and advise.