Error that I am getting
ReferenceError: LAST_PAYMENT_AMOUNT is not defined
part of nodejs code
const LAST_PAYMENT_AMOUNT = [LAST_PAYMENT_AMOUNT];
const LAST_PAYMENT_DATE = [LAST_PAYMENT_DATE];
const NEXT_PAYMENT_DATE = [NEXT_PAYMENT_DATE];
const accountIntentMap = {
payment_summary_payoff: {
LAST_PAYMENT_AMOUNT: "lastPaymentAmount",
You're trying to access: LAST_PAYMENT_AMOUNT, LAST_PAYMENT_DATE & NEXT_PAYMENT_DATE before initialization.
That code won't work, and doesn't make sense. You probably want to assign something different to each const.
Related
I have a query with the syntax in the require statement. Please refere the sample code below.
const nodemailer = require("nodemailer");
const {google} =require('googleapis');
const {OAuth2}=google.auth;
Some times , I see sample codes which use
const {<variable>} = require('moduleName')
Other times, I see like below
const <variable> = require('moduleName')
What is the difference between them?
Thanks in Advance.
Grateful to the Developers Community.
So, you use { } in this context when you want to do object destructuring to get a property from the exported object and create a module-level variable with that same name.
This:
const { google } = require('googleapis');
is a shortcut for this:
const __g = require('googleapis');
const google = __g.google;
So, within this context, you use the { google } only when you want the .google property from the imported module.
If you want the entire module handle such as this:
const nodemailer = require("nodemailer");
then, you don't use the { }. The only way to know which one you want for any given module is to consult the documentation for the module, the code for the module or examples of how to use the module. It depends entirely upon what the module exports and whether you want the top level export object or you want a property of that object.
It's important to realize that the { } used with require() is not special syntax associated with require(). This is normal object destructuring assignment, the same as if you did this:
// define some object
const x = { greeting: "hello" };
// use object destructuring assignment to create a new variable
// that contains the property of an existing object
const { greeting } = x;
console.log(greeting); // "hello
When you import the function with {}, it means you just import one function that available in the package. Maybe you have've seen:
const {googleApi, googleAir, googleWater} = require("googleapis")
But, when you not using {}, it means you import the whole package, just write:
const google = require("googleapis")
So, let say when you need googleApi in your code. You can call it:
google.googleApi
I'm trying to use this https://npm.runkit.com/globalpayments-api script but I can't figure what I'm doing wrong.
When I run the Runkit and add the first code to create a new Credit Card it throws error "ReferenceError: CreditCardData is not defined":
const card = new CreditCardData();
card.number = "4111111111111111";
card.expMonth = "12";
card.expYear = "2025";
card.cvn = "123";
How I can point CreditCardData to var globalpaymentsApi = require("globalpayments-api") which contains all this consts?
Demo: https://runkit.com/embed/8hidbubpbk8n
What I'm doing wrong?
Most likely in your code, function CreditCardData() doesn't exist - this means you didn't import it. Try adding this at the beginning of your .js file:
const { CreditCardData } = require('globalpayments-api');
Running some tests through Chai, I noticed the tests would fail under this code:
const add = require('./addition');
//'add is not a function error' even though it's directly exported as a function
But it would pass under this:
const {add} = require('./addition');
Yet when using npm modules, everything is declared without the brackets:
var express = require('express');
var app = express();
var session = require('express-session');
And those are essentially objects with multiple properties to be accessed. Why does it work this way? Is it only function exports that must be assigned as objects explicitly?
This is known as object destructuring. Please refer the link.
For example you have exported a file called sampleFunctions.js which has following functions as exports
function function1(params) {};
function function2(params) {};
module.exports = {
sampleFunc1: function1,
sampleFunc2: function2
}
Now when you need to require it, there are two ways -
when you only need one function(using object destructuring)
let {sampleFunc1} = require('./sampleFunctions');
sampleFunc1();
In this you exposed only the required function not all of the functions exported from that file.
when you want to require all the functions from that file
let sampleFuncs = require('./sampleFunctions');
let samFunc1 = sampleFuncs.sampleFunc1;
samFunc1()
I have problem using all 3 of the packages together. I define them like this:
var moment = require('moment-timezone');
var momentRange = require('moment-range');
And when I want to use the moment-range functions, I'm trying to call it like this:
var range1 = momentRange.range(moment("string1"), moment("string2"));
And I'm getting error: TypeError: momentRange.range is not a function
What am I doing wrong?
According to the documentation, you are supposed to use the moment-range library to first extend the core moment library itself, then use moment.range because the moment-range package adds additional functions to the moment object:
var momentRange = require('moment-range');
momentRange.extendMoment(moment);
moment.range(moment(…), moment(…)); // Now usable
Specifically, in their documentation:
CommonJS:
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);
I don't think I quite understand how exports work in Node.js. In a bit of sample code, I noticed the exports object used in this manner:
exports = mongoose = require('mongoose')
mongoose.connect(config.db.uri)
exports = Schema = mongoose.Schema
What is happening behind the scenes when you use exports = twice like that? Looks to me like "mongoose" should not be exported. I did this quick test:
var foo
, bar
exports = foo = 'foo'
exports = bar = 'bar'
// reports 'bar' only
console.log(exports)
and the second test does overwrite the first export.
My guess is the original author of that sample code is confused about module.exports vs exports. To use the exports object, you must add properties to it like this:
exports.size = 42;
If you re-assign the exports variable to a new object, you basically lose access to the global exports object that node.js provided for you. If you do this twice or three or N times, the effect is the same. It's useless. For example: mod_b.js
var realExports = exports;
realExports.height = 42;
var exports = {};
exports.weight = 43;
And in mod_a.js
var mod_b = require('./mod_b');
console.dir(mod_b);
Run node mod_a.js and you get:
{ height: 42 }
Notice height is there but weight is not. Now, what you can do is assign module.exports to be an object, and that is the object that will be returned when another module requires your module. So you will see things like.
var x = 10;
var y = 20;
module.exports = {x: x, y: y};
Which will do what you expect. Here's some informative articles on the details.
Node.js Module – exports vs module.exports
What is the purpose of NodeJS module.exports and how do you use it?
Mastering Node
As an extension to #Peter-Lyons answer, your example is valid, your test case is incorrect.
Exporting variables like this is not recommended, but by doing that, you are exposing this variables into a global namespace.
e.g. test0.js
'use strict';
var assert = require('assert');
var test1 = require('./test1');
assert(globalVar1 === 123);
assert(globalVar2.a === 123);
test1.js -- mind the missing 'use strict';
exports = globalVar1 = 123;
exports = globalVar2 = {a:123};
Once again, it's valid, but not recommended.
Cheers.