Undefined results on calling module.exports in nodejs - javascript

my app.js contents:
var greet3=require('./greet3');
greet3.greet();
my greet3.js contents
function Greetr(){
this.greeting='greet3';
this.greet=function(){
console.log(this.greeting);
}
}
module.exports=new Greetr();
my console window output : greet3
but when i change the contents of app.js as :
var greet3=require('./greet3');
greet3();
i get the ouput as 'undefined'
I am a beginner in node.js and was trying different module patterns in it.I need help on why I am getting the result as undefined

Given your above module, the line
var greet3=require('./greet3');
essentially becomes:
var greet3 = {
greeting: "greet3",
greet: function(){
console.log(this.greeting);
}
}
So in app.js, greet3 is now an Object. greet3.greet(); works as expected and logs "greet3".
But greet3() produces greet3 is not a function, since greet3 isn't a function.

You are exporting a initialised object, when you set var greet3=require('./greet3') and call greet3() you are trying to use the object as a function, hence it says undefined.

Change your code in greet3.js:
module.exports = function(){
this.greeting='greet3';
this.greet=function(){
console.log(this.greeting);
}
}

As others said before, your code is working as expected.
Your module:
function Greetr(){
this.greeting='greet3';
this.greet=function(){
console.log(this.greeting);
}
}
module.exports=new Greetr();
Returns an object, but I think you wanted to return a function, so the last line should look like this:
module.exports=Greetr;
Instead of this:
module.exports=new Greetr();
Because now when you call:
var greet3=require('./greet3');
greet3();
It assigns object of function Greetr to a variable greet3 and you cannot call an object.

Related

TypeError: object.function is not a function

I have 2 files QueryExecution.js and app.js; from app.js am calling a function query_result which is in QueryExecution.js. But when I made the call in app.js am getting object.function is not a function.
Kindly apologize for any stupid mistakes :( Thanks in advance for your time and help
I tried to print the object(realtime_chart_new) that I created to call the function. the object is printing the entire function content.
QueryExecution.js
function query_result(connection,sql) {
.......query statements and processing content .....
return CountOrders, TotalAmt;
}
module.exports = query_result;
app.js
var connection = {
.....connection statements .....
};
var realtime_chart_new = require('./QueryExecution.js');
var SalesCountOrders, SalesTotalAmt = realtime_chart_new.query_result(connection,sql);```
#Below is the error statements:
var SalesCountOrders, SalesTotalAmt = realtime_chart_new.query_result(connection,sql);
TypeError: realtime_chart_new.query_result is not a function
You have overridden module exports with query_result, so now require('./QueryExecution.js') returns a function.
This will give you expected result:
module.exports.query_result = query_result;
OR
module.exports = {
query_result: query_result
};

TypeError: undefined is not an object when unit testing with jasmine

I'm trying to write a unit test for a function and I'm getting an error. I'm also unsure how to test other parts of the function correctly.
private dictionaryMap (loggedIn, response) {
const translations = this.convertToArrays(response.data.translations);
this.configureMomentLocale(language);
if (!loggedIn) {
this.cachePublicDictionary(translations);
}
// not testing this part
this.dictionary = new Dictionary({
translationMap: Object.assign({}, this.getPublicDictionaryFromCache() || {}, translations),
});
return this.rx.Observable.of(this.dictionary);
}
And my unit test so far looks like this:
describe('dictionaryMap', () => {
it('calls configureMomentLocale()', () => {
const foo = {
'foo':'bar',
};
spyOn(service, 'configureMomentLocale');
service.dictionaryMap({}, false);
expect(service.configureMomentLocale).toHaveBeenCalled();
});
});
And when I run this test I get this error:
TypeError: undefined is not an object (evaluating 'response.data.translationMap')
Do I need to mock response.data.translations or assign the json structure? (translationMap: {'email': 'email', 'forgotPassword': 'Forgot password?'})
Also, I'm not sure how to properly test the other parts of the function, like the if statement or returning the observable. I am new to unit testing.
Your method dictionaryMap accepts 2 parameters - 1st is loggedIn (presumably boolean) and the 2nd one is response. On the first line of that method (before calling configureMomentLocale) you have a line const translations = this.convertToArrays(response.data.translations); which expects the response variable to have a property named data.
In your test, you have 2 errors on the line service.dictionaryMap({}, false);:
You're setting the arguments in reverse order - you should put the boolean argument first and the object one second
The object doesn't have a property named data
The line should be corrected to be something similar to service.dictionaryMap(false, { data: {} });. You might even need to define translations property for data object - it really depends on what this.convertToArrays function does and how it handles undefined values.

Many new instances of requirejs module

I am creating a typing game which I have "app.js" as a main and loading "words.js" by requirejs.
I need to use > 2 words but I am still naive with javascript and not sure this is right to do in AMD. Anyone could point me out. I would really appreciate it.
I think it would be like following code but it doesn't work and give me error
"Uncaught TypeError: object is not a function"
[app.js]
require(['jquery','app/canvas','app/words'], function($,game_screen,words){
var words1 = new words();
var words2 = new words();
.
.
});
[words.js]
define(['app/canvas'],function(canvas){
var word_list=[{"word1"},{"word2"},...];
return {
addWord: function(new_word){
.
.
});
Right now you're returning an object from your words module: { addWord: function() {} ... }. So in app.js, when you set words to be equal to the object returned from the words module, you would invoke the functions by doing words.addWord().
If instead you want to use the new words() syntax in app.js, you would have to change your words module to return a function instead of an object (hence the error):
define(['app/canvas'],function(canvas) {
var word_list=[{"word1"},{"word2"},...]; // not sure how this is used
function words() {
// some code
}
words.prototype.addWords = function() {
// some code
}
return words;
}

TypeError when using Jquery and Object property function

I'm getting an error when using jquery and I would like to know its cause:
here is part of my code
function Wbook(name){
this.name = name;
}
Wbook.prototype.GetHTML = function() {
Object.defineProperty(this, "GetHTML", {enumerable : false,
configurable : true});
var html ='<h1>TEST1</h1>';
return html;
};
var rs = {};
rs.WB = new Wbook('test1');
var foo = rs.WB.GetHTML();
$(foo).appendTo('div#id1'); // This works
$(rs.WB.GetHTML()).appendTo('div#id1');
// This doesn't work >> TypeError: rs.WB.GetHTML is not a function
I can also getting to work if I comment the Object.defineProperty section, so I'm suspecting this might have to do with the enumerability, but I'm not sure of it
//Edit: While creating Jfiddle, I notice rs.WB.GetHTML() is always failing the second time it runs :-/. (It works fine if I comment the Object.defineProperty section)
The first time you call .GetHTML() it's returning some HTML, but in the process the Object.defineProperty call is overwriting the .GetHTML method with a new property that has no value.
It's therefore unsurprising that on the second invocation you get an error because the value of .GetHTML by then is undefined.
If the intent of your code is to ensure that GetHTML is not enumerable, use the following code which directly adds the method to Wbook.prototype and (automatically) sets it non-enumerable:
Object.defineProperty(Wbook.prototype, 'GetHTML', {
value: function() {
...
}
});

Problem with Javascript object and accessing property which exists

I have something like this:
var test = {};
function blah() {
test[2] = 'filled';
}
blah(); // ! Hopefully confusion is now averted..
console.log(test);
//result test -> 2:"filled"
console.log(test[2]);
//result undefined
I don't understand why I'm getting 'undefined' in the second instance when according to the first instance, the property of that object clearly exists!
Does anyone have any ideas?
Thanks
OK, it seems that folk are getting confused as to what context the code exists in, for clarity sake I have now added the call to the blah(). but please refer to the comment under Jeff B's response!
Here is an example of relevant code so to say:
mydb = ..... //gets created here with relevant credentials
var test = {};
mydb.transaction(
function(transaction) {
transaction.executeSql("select * from mytable;", [], function(transaction,result) {
var r = result.rows.item(0);
test[2] = r.title;
}, errorHandler);
});
console.log(test);
//result test -> 2:"the title"
console.log(test[2]);
//result undefined
#Dancrumb
Your mention of the single-threadedness of Javascript gave me an idea, and I tried this:
window.setTimeout(function(){ alert(test[2]); },2000);
and it worked! I got the expected value to alert. Can you suggest how I can get around this without using a 'hack' like that above?
Because you aren't calling blah()?
Also, you want:
var test = [];
or:
var test = new Array();
EDIT
I ran the following code:
mydb = openDatabase('note','','Example',1024);
var test = {};
mydb.transaction(
function(transaction) {
transaction.executeSql("select * from mytable;", [], function(transaction,result) {
var r = result.rows.item(0);
test[2] = r.title;
}, errorHandler);
});
console.log(test);
console.log(test[2]);
in Safari 4.0.5
I got the following:
Object
No Properties
undefined
This is what I would expect to see. The object test does not have any properties assigned to it until the callback from mydb.transaction occurs and, since Javascript is single threaded, this cannot happen before the calls to console.log.
Since you're getting a different outcome, can you outline what browser and what version you are using?
This is pretty clearly an asynchronous issue. The simplest way of getting code to run after you set test[2], is to either put the code right there, or use another callback, and call it after you set test[2].

Categories