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;
Related
I have a typescript application running on node.
I am using 'EventEmitter' class to emit a change in variable value.
This is my piece of code,
import events from 'events';
public async updateStream(streamContext: string, state: boolean): Promise<string> {
const eventEmitter = new events.EventEmitter();
if (state === true) {
return StreamManagement.instance.activeStreams.get(streamContext).streamState = 'Paused';
} else {
const streamState = StreamManagement.instance.activeStreams.get(streamContext).streamState = 'Active';
eventEmitter.emit('resume');
return streamState;
}
}
public async waitForStreamActive(stream: Stream) {
const eventEmitter = new events.EventEmitter();
// tslint:disable-next-line:no-unused-expression
return new Promise(( resolve ) => {
eventEmitter.on('resume', resolve );
});
}
This piece of code builds fine. But when i run the code, as in execute the operation, I am getting the following error,
error: errorHandler - Apply - Hit Unhandled exception {"timestamp":"2019-04-29T12:33:49.209Z"}
error: errorHandler - Apply - Cannot read property 'EventEmitter' of undefined - TypeError: Cannot read property 'EventEmitter' of undefined
at StreamResource.updateStream (C:\Vertigo\core\reference_platform\dist\index.js:10695:51)
at StreamService.patchStream (C:\Vertigo\core\reference_platform\dist\index.js:22524:40)
at process._tickCallback (internal/process/next_tick.js:68:7) {"timestamp":"2019-04-29T12:33:49.215Z"}
What am I doing wrong?
I've set up minimal project to reproduce it and immediately ts compiler warns me about:
TS1192: Module '"events"' has no default export.
But this seems to work:
import * as EventEmitter from 'events'
new EventEmitter();
I'm using this handlebars-loader for webpack 4. Now I wanted to use a custom helper, but i only get this error
ERROR in Template execution failed: TypeError: __default(...).call is not a function
ERROR in TypeError: __default(...).call is not a function
this is my webpack.config
//handlebars-loader
{
test: /\.(hbs)$/,
loader: "handlebars-loader",
options: {
helperDirs: [path.join(__dirname, './src/hbs/helpers')],
partialDirs: [path.join(__dirname, './src/hbs/partials')]
},
}
and this is my simple helper
const Handlebars = require('handlebars');
Handlebars.registerHelper('repeat', function(n) {
console.log(n);
});
and how I use it
{{#repeat 10}}
<span> repeat</span>
{{/repeat}}
Does anyone know what I'm doing wrong?
I found a solution. I have to export the helper as a module, then it works.
//repeat.js
module.exports = function(times, options) {
console.log(times);
};
Thank you, Gregor! This has been frustrating me all day!
I had a simple function in a helper file, checkActive.js :
const Handlebars = require("handlebars")
Handlebars.registerHelper("checkActive", function (pageName, linkedPageName) {
return pageName === linkedPageName ? "active" : ""
})
This would not work and gave me this error:
ERROR in Template execution failed: TypeError: __default(...).call is not a function
ERROR in TypeError: __default(...).call is not a function
I found your solution and tried it. Lo-and-behold it worked! This is what I am now using instead:
module.exports = function (pagename, linkedPageName) {
return pagename === linkedPageName ? "active" : ""
console.log(pagename)
};
In order for this to work, you need to have the function saved in a file with the same name as the handlebars helper function you are calling, ie. in my case: checkActive.js
I am getting error when I execute my code. Below code used for creating collection in cloud Firestore, before this I didn’t created any collection in cloud Firestore. It is empty. In the console.log, I am getting correct json object.
createEntry(subjectId: string, questionnaire: string, callback: (err: any, response: any) => void) :void {
let testEntity: TestEntity = new TestEntity(subjectId, questionnaire);
console.log("testEntity.toJSON();----------->",testEntity.toJSON());
var docRef = this.dbManager.collection('testQuestion').doc(subjectId);
var setAlan = docRef.set(testEntity.toJSON());
}
Here TestEntity is json values as below. All the fields have values.
public toJSON(): testEntry {
let returnJSON = {
"entry_id": this.entry_id,
"subject_id": this.subject_id,
"entry_date": this.entry_date,
"questionnaire": this.questionnaire,
"entry_start_timestamp": this.entry_start_timestamp,
"entry_end_timestamp": this.entry_end_timestamp,
"entry_complete": this.entry_complete,
"responses": this.responses,
"last_answered_question" : this.last_answered_question,
"entry_status" : this.entry_status
}
return returnJSON;
}
dbManager is this.adminInstance:
export class DatabaseManager{
private static adminInstance:any;
static getAdminInstance(): any {
if(!this.adminInstance)
admin.initializeApp(functions.config().firebase);
this.adminInstance = admin;
return this.adminInstance.firestore();
}
}
I am getting below error. How to solve it?
Error: Invalid use of type "undefined" as a Firestore argument.
at Object.exports.customObjectError.val [as customObjectError] (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/validate.js:164:14)
at Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/document.js:813:20)
at Function.encodeFields (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/document.js:683:36)
at Function.fromObject (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/document.js:223:55)
at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/write-batch.js:301:39)
at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/src/reference.js:420:8)
at testEntryService.createEntry (/user_code/services/test-entry-service.js:19:30)
at /user_code/services/intentService.js:22:36
at Function.<anonymous> (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:146:23)
at next (native)
Since I am new to Firebase, I am unable to identify this error. What is the root cause of the issue?
The following code works perfectly fine when I use bluebird promises:
import * as Promise from 'bluebird';
getAccount(id) {
var account = find(accounts, ['id', id]);
return account ?
Promise.resolve(account) :
Promise.reject(new NotFoundError());
}
NotFoundError is defined as follows:
export function NotFoundError(message = 'Not Found') {
this.name = 'NotFoundError';
this.message = message;
this.stack = (new Error()).stack;
}
NotFoundError.prototype = Object.create(Error.prototype);
NotFoundError.prototype.constructor = NotFoundError;
However, if I remove the import of bluebird in getAccount() and let node.js take over promises, the code fails inside the NotFoundError() constructor because this is not defined. Specifically, the constructor is called twice, once correctly from the getAccount() code shown above and a second time by node.js's _tickCallback() function with this as undefined:
NotFoundError (errors.js:13)
runMicrotasksCallback (internal/proces…ext_tick.js:58)
_combinedTickCallback (internal/proces…ext_tick.js:67)
_tickCallback (internal/proces…ext_tick.js:98)
Why is node.js calling the NotFoundError() constructor a second time and that too incorrectly!!!
Please help.
The issue is caused by this line:
.catch(NotFoundError, function() { ... })
Native promises don't have an option to pass a specific error class to a catch method, so what happens is that when an error occurs, NotFoundError is called (without a new in front of it) because it's presumed to be the catch handler.
I am requiring a module and saving it in a variable. But when I call a module function by wrong name, it does not throw any error or consoles any error. How do I make this throw error?
var module = require('../pre_process/' + preProcessFolder + '/' + preProcessModule);
// module -> { XYZ: [Function] }
//Following does not throw error and doesn't console anything.How to handle/debug this error
module['XY'](result, userId)
.then(function(recData) {
})
I am using q library for promise.
So you want to check if a function (provided by a modul) exists.
You could use try like the example here:
Javascript check if function exists