Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the function.caller on nodejs

I have a task that depends upon function.caller to sanity check that a caller is authorized.

According to this url, caller is supported in all major browsers... and all of my unit tests pass:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller

However, nodejs rejects all attempts to access function.caller (reports it as null).

I'm open to suggestions to make this work on nodejs... I'd hate to have this framework only work on browsers.

Thanks!

like image 649
Michael Avatar asked Sep 11 '14 05:09

Michael


People also ask

How do you call a function in node JS?

A function can be executed by calling the execute() method in which the function ID and configuration (of type JSON) are passed as parameters. The functions reference used in the code snippets below is the component instance.

How do you find out the caller function in JavaScript?

Using the function.caller property in JavaScript. It returns the name of the function from which the current function is invoked. It returns the null value if the function is invoked from the top-level JavaScript code.

What is the caller of a function?

The function. caller property of the JavaScript function object returns the function that invoked the specified function. It returns null if the function “f” was invoked by the top-level code in JavaScript. For functions like strict functions, async functions and generator function this method returns null.

How do you call a function after some time in node JS?

setTimeout() can be used to schedule code execution after a designated amount of milliseconds. This function is similar to window. setTimeout() from the browser JavaScript API, however a string of code cannot be passed to be executed.


1 Answers

The Caller-id npm package makes it as easy as pie : https://www.npmjs.com/package/caller-id from the docs :

var callerId = require('caller-id');

// 1. Function calling another function
function foo() {
    bar();
}
function bar() {
    var caller = callerId.getData();
    /*
    caller = {
        typeName: 'Object',
        functionName: 'foo',
        filePath: '/path/of/this/file.js',
        lineNumber: 5,
        topLevelFlag: true,
        nativeFlag: false,
        evalFlag: false
    }
    */
}
like image 54
eff_it Avatar answered Oct 21 '22 07:10

eff_it