Node Minor Code Issues - Require and Exported Functions - javascript

In a Previous Commit:
We Call A Function like So var previousRoute = appRouter.getPreviousRoute();
Where appRouter is var appRouter = require("app_utilities/default/app-router");
and app-router contains an export as the following:
module.exports = {
getPreviousRoute: getPreviousRoute
}
function getPreviousRoute() {
return window.appPreviousRoute;
};
however in a latter commit the following line errors:
var previousRoute = appRouter.getPreviousRoute();
The Error would be: Uncaught TypeError: appRouter.getPreviousRoute is not a function
and we have to change it to:
var previousRoute = appRouter.getPreviousRoute;
I would like to know what would make it that we would need to drop the parentheses?
I have ran:
node -p process.versions.v8
6.8.275.32-node.36

Most likely as you have declared a variable not the type function to export and the variable inside export holds the reference of the function so if you directly access getPreviousRoute a function it will produce an error as you did not export a function and the program will not find that. So in terms of working when a variable is called the program will find that its declared then will look for its reference function that you gave and will execute it
Instead if you will export like
exports.getPreviousRoute = ()=>{}
It will not show you an error as its a type of function and will be accessible let me also point if i am wrong

Related

Micro:bit Extension "Program Error: Dereferencing Null/Undefined values"

I am writing an extension for Micro:Bit that controls an OLED. I have an array that I declare outside of any function. When I try to use it inside of a function I get the error "Program Error: Dereferencing Null/Undefined Value" in an orange pop-up.
As far as I can tell, the program does not recognize the array name inside of any function, and I'm able to create a new array with that name as if it were out of scope. Running the same code directly below the initial declaration (outside of any function) behaves as expected
Below is the simplest version I could make that still produces the error:
//% color="#00CC99"
namespace OLED_Test {
let screenBuffer = [0x00]
//block
export function init() {
let x = screenBuffer[0]
}
}
So, I still don't know why it doesn't work, but I discovered that if I declare the variable in the namespace and then set it inside of an init function, the variable will be have appropriately for the rest of the code. As such I just put all of my initial variable assignments inside of my init function, e.g:
//% color="#00CC99"
namespace OLED_Test {
let screenBuffer: Array<number>
//block
export function init() {
screenBuffer = [0x00]
}
export function foo(){
let x = screenBuffer[0]
}
}

Unable to export variable from node js module

First of all I have checked all the questions on Stack Overflow regarding this.
I am trying to export variable str whose value is getting updated inside the function to another module but it is showing undefined after exporting it in another file.
But if I update the value of variable outside function then export is working fine.
I have a function with code in file Excel.js
var str='abc';
wb.xlsx.readFile(filePath).then(function()
{
var sh = wb.getWorksheet("Sheet1");
console.log("YOUR LECTURE IS",sh.getRow(goingSlot+1).getCell(DAY+1).value);
//console works fine
str="YOUR LECTURE IS"+sh.getRow(goingSlot+1).getCell(DAY+1).value;
//the assignment here leads to undefined after exporting
}
str="something";
//this successfully exports the value as something
And then I export this to my main file with syntax
exports.str=str;
Incase you need to see code for main file
The code for the main file is
const express=require('express');
const app=express();
const myle=require('./readingExcel.js');
const res=myle.name;
console.log(res);
//CONSOLE SHOWS UNDEFINED
exports.str and str are not the same variable even though you write exports.str = str
var a = 2;
var b = a;
a = 4;
console.log(b) // b is still 2 and not 4
So use exports.str directly instead of str.
exports.str = 'abc'
// ...
exports.str="YOUR LECTURE IS"+sh.getRow(goingSlot+1).getCell(DAY+1).value;
// ...
exports.str = 'something'
I just want to add something to #t.niese's response (which in my opinion is pretty solid).
Check your code for any mode.exports happening after your exports.str assignment. exports and module.exports are usually pointing to the same place, but I always use module.exports. Using exports has the risk of being overwritten somewhere else (module.exports too, but that is the real reference used by node).
https://medium.freecodecamp.org/node-js-module-exports-vs-exports-ec7e254d63ac

Cannot access function inside function javascript

I'm trying to clean my code a bit, so I create some small objects or libraries (call it how you want) like:
function myLib() {
this.get = function() { ... };
...
};
Problem is when I try to call it like myLib.get(). It throws the following error:
Uncaught TypeError: myLib.get is not a function
I've tried to encapsulate the call into $(document).ready(), but it did not help.
Can you help me, please?
Thanks!
myLib is used for "libary", and you want to call this "get" method of libary.
Static instance is better in your case.
const myLib = {
get:function(){},
get2:function(){}
...
};
myLib.get();
myLib.get2();
so I create some small objects or libraries (call it how you want)
In your case you are creating a constructor, myLib is a constructor and not just a function, and you can't access a function's properties and methods directly that's why you got the Exception.
So you need to get an instance of myLib in order to call the get method or to access any of its members(methods).
function myLib() {
this.get = function() { console.log("get called!!"); };
};
let lib = new myLib();
lib.get();
Note:
And from the MDN Reference for Functions you can see that:
The this keyword does not refer to the currently executing function, so you must refer to Function objects by name, even within the function body.
You should use myLib as a constructor ie:
var lib = new myLib();
lib.get();

TypeError: BookList is not a constructor

I have code js in my template in django app:
bookList = new BookList();
bookList.init();
updateBookUrlList();
And in this code I get an error: Uncaught TypeError: BookList is not a constructor
I have this code in external file book-list.js:
var BookList=function(){};
BookList.prototype.init=function(){this.$eventslist=$(".event-list"),...
I can't figure out what is wrong with my code.
EDIT:
I changed my code to:
function BookList() {};
But now I get error: TypeError: bookList.init is not a function
Only change your function declaration from:
var BookList = function () {
To:
function BookList() {
When you assign a function to a variable, this can't be called before its declaration. But with the 2nd way, the function will be available from any part of your code, even before its declaration.

Can't access module object properties from main in Screeps

I have the following simple test module (called testModule) in Screeps:
module.Exports = {
myProperty:'test'
};
In main, I try to output the contents of the module like so:
var x = require('testModule');
console.log("Value:" + JSON.stringify(x));
But all I get is an empty object ({});
As a result, x.myProperty is undefined. I have also tried making the module into a function, like so:
module.Exports = function(){
return {
myProperty:'test'
};
};
Then assigning it to x with var x = require('testModule')(); but I get the same result.
Obviously the game is still in development, so it is possible that this is a bug, but I'd like to rule out a mistake on my part first. Anyone been able to achieve what I'm trying to do? Anyone see what I'm doing wrong?
Edit
Interestingly, it gives me the same empty object even if I change the module to this:
module.Exports = 'test';
Surely this should be printing the string 'test' rather than an empty object? Is this a quirk of require js?
Just figured this out - I was using an uppercase E in module.exports. I have corrected the case and now it works fine:
module.exports = {
myProperty:'test'
};

Categories