Ternary operator not behaving as intended - node.js

my Code link
on line currentEnvironment = typeof (process.env.NODE_ENV === 'string') ? currentEnvironment.toLowerCase() : '';
when I pass the input 🌈 uptime-monitoring-app >> NODE_ENV=production node app.js it works without any issue.
Command Line arguments :
0: /usr/local/Cellar/node#10/10.15.0/bin/node
1: /Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/app.js
string
Server Started # 5000 Environment : production
--------------------------------------------------------------------------------------------------
BUT for the input uptime-monitoring-app >> Nnode app.js it throws error, because now the NODE_ENV variable is undefined now.
Command Line arguments :
0: /usr/local/Cellar/node#10/10.15.0/bin/node
1: /Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/app.js
undefined
/Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/envConfig.js:66
currentEnvironment = typeof (process.env.NODE_ENV === 'string') ? currentEnvironment.toLowerCase() : '';
^
TypeError: Cannot read property 'toLowerCase' of undefined
at Object.<anonymous> (/Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/envConfig.js:66:86)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/tentacion/Documents/CODE/Node without NPM/uptime-monitoring-app/app.js:11:19)
at Module._compile (internal/modules/cjs/loader.js:689:30)
🌈 uptime-monitoring-app >>
What I expected
if the NODE_ENV variable is undefined then the expression typeof (process.env.NODE_ENV === 'string') would be false so the next statement should be executed. ie assigning it with the empty string.
Please help, I don't know what am I doing wrong. I am new to node

process.env.XXX are always either a string or undefined.
Prefer something like const { XXX = 'some-default-value' } = process.env, which is called object destructuring, which will either use the provided value for XXX or give it a default value if it is undefined.

Related

NodeJS SyntaxError: Unexpected token '.'

Beginner to Node JS and NPM. The following code throws a Syntax error and can't figure out the issue.
Node version: v12.18.3
NPM version: 7.15.1
Error
LayerObj.options?.["displayName"] != undefined
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Applications/MAMP/htdocs/nft-generators/hashlips_art_engine-main/index.js:2:39)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
Code
const layersSetup = (layersOrder) => {
const layers = layersOrder.map((layerObj, index) => ({
id: index,
elements: getElements(`${layersDir}/${layerObj.name}/`),
name:
layerObj.options?.["displayName"] != undefined
? layerObj.options?.["displayName"]
: layerObj.name,
blend:
layerObj.options?.["blend"] != undefined
? layerObj.options?.["blend"]
: "source-over",
opacity:
layerObj.options?.["opacity"] != undefined
? layerObj.options?.["opacity"]
: 1,
bypassDNA:
layerObj.options?.["bypassDNA"] !== undefined
? layerObj.options?.["bypassDNA"]
: false,
}));
return layers;
};
Optional chaining (?.) was added in Node.js 14 and is not directly available in Node 12.
The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.
Use a more recent version of Node or use Node 12 compatible code.
New JS features in Node.js v14.0 discusses some new language constructs in Node 14.

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object

For
const systemRegex = /^system\./,
endOfLine = require('os').EOL,
EJSON = require('mongodb-extjson');
I got error at the line EJSON = require('mongodb-extjson'), details is as follows:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received function hidden
at Function.from (buffer.js:331:9)
at fnv1a32 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:21:25)
at fnv1a24 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:39:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/objectid.js:14:20)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/bson.js:7:14)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19) {
code: 'ERR_INVALID_ARG_TYPE'
}
How to resolve it?
It is because of the Node.js version.
Node.js v10.15.1: Works!
Node.js v12.8.1: Error loading the module
I am not sure about the cause, but I would guess that it is because Node.js' packaging mechanism has undergone a change. And mongodb-extjson has not been modified to adhere to the new mechanism. As mentioned in their GitHub repository, it is unlikely to be fixed as it is no longer being maintained.
You have the following choices:
Downgrade to v10 of Node.js, OR
Use the recommended bson module instead:
$ npm install bson
const { EJSON } = require('bson');
const data = '{ "someId": { "$oid": "5ec7cb151a1878fbefce4119" } }'
const doc = EJSON.parse(data, { relaxed: false });
console.log(doc.someId._bsontype)
// Should print 'ObjectID'
Note the difference in the EJSON option between the two modules. Whereas in mongodb-extjson the default is strict, in bson the default is relaxed.

firebase-fuctions ,TypeError: Cannot read property 'onRequest' of undefined

updated firebase via npm i -g firebase-tools ,
used my firebase id ,
while deploying my function at firebase ( with firebase deploy ) this error results out
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'onRequest' of undefined
at Object.<anonymous> (C:\Users\umer\Desktop\learning chatbots\usingFulfilmentAsWebhook\functions\index.js:9:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:15:15
at Object.<anonymous> (C:\Users\umer\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:53:3)
here is the code
const functions = require('firebase-functions');
exports.webhook = functions.http.onRequest((request,response)=>{
console.log("request.body.result.parameters", request.body.result.parameters);
let params = request.body.result.parameters;
response.send({
speech: `${params.name} your hotel booking request for ${params.roomType} room is forwarded for ${params.persons} persons. We will contact you on ${params.email} soon`
});
});
try after putting "s" in http in below line
exports.webhook = functions.https.onRequest((request,response)=>{
...
}
Add this in your first line
const functions = require('firebase-functions');
Besides, make sure you have necessary packages installed.

Attempting to list noflo node modules leads to Syntax Error

I'm following the documentation
on how to get NoFlo up and running. Just prior to the section
"Defining your first graph" there is a line which says that I can see
the list of installed componenents by typing
$ ./node_modules/.bin/noflo list .
However, doing so leads to the stack backtrace below. The first few
lines of the program that noflo is barfing on are:
if typeof process isnt 'undefined' and process.execPath and process.execPath.indexOf('node') isnt -1
noflo = require "../../lib/NoFlo"
else
noflo = require '../lib/NoFlo'
class Graph extends noflo.Component
constructor: ->
#network = null
#ready = true
#started = false
#baseDir = null
#loader = null
Transcript of shell session follows
tbrannon#tbrannon0:~/Documents/Programming/noflo/my-noflo-example-app$ ./node_modules/.bin/noflo list .
/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/src/components/Graph.coffee:1
(function (exports, require, module, __filename, __dirname) { if typeof proces
^^^^^^
SyntaxError: Unexpected token typeof
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at ComponentLoader.load (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/lib/ComponentLoader.js:127:26)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/bin/noflo.js:115:36
at Function..each..forEach (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/underscore/underscore.js:87:22)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/bin/noflo.js:113:18
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/lib/nodejs/ComponentLoader.js:132:16
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/underscore/underscore.js:758:21
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/lib/nodejs/ComponentLoader.js:152:18
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/lib/nodejs/ComponentLoader.js:39:16
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/underscore/underscore.js:758:21
at ComponentLoader.getModuleComponents (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/lib/nodejs/ComponentLoader.js:61:16)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/lib/nodejs/ComponentLoader.js:146:22
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:118:5
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at cb (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:48:11)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at cb (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:48:11)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at cb (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:48:11)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at cb (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:48:11)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at cb (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:48:11)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at cb (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:48:11)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:234:14
at asyncMap (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/node_modules/slide/lib/async-map.js:27:18)
at next (/usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:200:5)
at /usr/local/g7/home/tbrannon/Documents/Programming/noflo/my-noflo-example-app/node_modules/noflo/node_modules/read-installed/read-installed.js:157:7
at LOOP (fs.js:1356:14)
at process._tickCallback (node.js:415:13)
tbrannon#tbrannon0:~/Documents/Programming/noflo/my-noflo-example-app$
This issue is caused by an API change in CoffeeScript 1.7. A fix was made in NoFlo's git repository.
The fix was included in NoFlo 0.4.4 available via NPM.

Node.js with ExpressJS error: Cannot read property 'prototype' of undefined

Running node.js v0.10.2 and express v3.1.1 (latest at this time) and getting this error:
/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10
window.XMLHttpRequest.prototype.withCredentials = false;
^
TypeError: Cannot read property 'prototype' of undefined
at create (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10:26)
at /root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9503:18
at Object.<anonymous> (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9505:2)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/root/dmr-addresses/address/log.js:1:71)
line 1 of log.js is:
var $ = require('jquery');
I've tried running npm install jquery but it has not fixed the problem.
Check this:
Same error here...
I don't know what I'm doing, but I changed the node-jquery.js fourth-fifth row's and it's start working :)
old:
if(window == null ) {
window = require('jsdom').jsdom().createWindow();
new:
if(!window || !window.document) {
window = require('jsdom').createWindow();
window.document = require('jsdom').jsdom();
You don't actually have a prototype object in Node server code, it's all stored in the much nicer __proto__ object and you should be using Object.create/defineProperty.
What exactly are you trying to do? Run an ajax query with Node? If so, you should be using Nodes http.request
An example could be:
require('request').post({
"uri" : "http://example.com/",
"headers" : {
'content-type': 'application/json'
},
"body" : "hello=world"
},
function(e,r,b){
// e = errors, r = response and b = returned body
console.log(b,r.statusCode));
});
Looks like this is an issue with the jsdom module that node-jquery depends on. It appears that this is a known issue, and that it has been fixed, but not published to npm yet.
Check it out: https://github.com/coolaj86/node-jquery/issues/52

Resources