简体   繁体   中英

Arangosh nor Web UI can access lodash module

The following is the user function I created:

// lodash-set.js
'use strict';

var _ = require('lodash');

function lodashSet (obj, path, value) {
  return _.set(obj, path, value);
}

module.exports = lodashSet;

It uses the lodash module that comes with Arango.

Within arangosh I perform the following:

> db._useDatabase("mydb");
mydb> var func = require("lodash-set.js");
mydb> var aqlfunctions = require("@arangodb/aql/functions");
mydb> aqlfunctions.register("LODASH::SET", func, true);
mydb> func({}, 'a.0.b', 123)
{ 
  "a" : [ 
    { 
      "b" : 123 
    } 
  ] 
}
mydb> db._query("RETURN LODASH::SET({}, 'a.0.b', 123)");
JavaScript exception in file '/usr/local/opt/arangodb/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1583: AQL: in function 'LODASH::SET()': user function runtime error: ReferenceError: _ is not defined
    at Object.lodashSet ((user function LODASH::SET):2:10)
    at FCALL_USER (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:227:55)
stacktrace of offending AQL function: ArangoError
    at THROW (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:51:13)
    at FCALL_USER (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:229:5) (while executing)
!      throw error;
!      ^
stacktrace: ArangoError: AQL: in function 'LODASH::SET()': user function runtime error: ReferenceError: _ is not defined
    at Object.lodashSet ((user function LODASH::SET):2:10)
    at FCALL_USER (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:227:55)
stacktrace of offending AQL function: ArangoError
    at THROW (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:51:13)
    at FCALL_USER (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:229:5) (while executing)
    at Object.exports.checkRequestResult (/usr/local/opt/arangodb/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoStatement.execute (/usr/local/opt/arangodb/share/arangodb3/js/client/modules/@arangodb/arango-statement.js:171:12)
    at Proxy.ArangoDatabase._query (/usr/local/opt/arangodb/share/arangodb3/js/client/modules/@arangodb/arango-database.js:946:42)
    at <shell command>:1:4

From Web UI

RETURN LODASH::SET({}, 'a.0.b', 123)

returns

Query: AQL: in function 'LODASH::SET()': user function runtime error: ReferenceError: _ is not defined at Object.lodashSet ((user function LODASH::SET):2:10) at FCALL_USER (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:227:55) stacktrace of offending AQL function: ArangoError at THROW (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:51:13) at FCALL_USER (/usr/local/opt/arangodb/share/arangodb3/js/server/modules/@arangodb/aql.js:229:5) (while executing)

As you can see, I can successfully use the function, lodashSet directly within Arango Shell and register the user function as LODASH::SET . However, if I invoke a query using LODASH::SET I get an exception stating that the _ is undefined.

I get the same undefined issue when I execute the user function from the Web UI.

What am I missing?

The reference to lodash needs to be inside the function. Like so:

// lodash-set.js
'use strict';


function lodashSet (obj, path, value) {
  var _ = require('lodash');
  return _.set(obj, path, value);
}

module.exports = lodashSet;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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