Creating empty array in class of JavaScript giving syntax error - javascript

I have two files in the same directory as 06classjs.js and 07 classObjects.js
06classjs.js
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
courseList = [];
getInfo() {
return { name: this.name, email: this.email };
}
enrollCourse(name) {
this.courseList.push(name);
}
getCourseList() {
return this.courseList;
}
}
module.exports = User;
07 classObjects.js
const User = require("./06classjs.js");
const dilip = new User("Dilip", "dilip#gmail.com");
console.log(dilip.getInfo());
dilip.enrollCourse("React");
When I am trying to run 07 classObjects.js using node it gives a syntax error for array declaration.
Error log
courseList = [];
^
SyntaxError: Unexpected token =
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/media/dilip/4E622DC7622DB49D/Courses/LCO/JSTube/06 Advanceish/07 classObjects.js:3:14)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
Can anyone help me with this?

You seem to be running an outdated version of node.js.
Support for public instance class fields was added in version 12.4.0.

Related

Discord.js v12: Error: The class/prototype returned from the extender function must extend the existing structure class/prototype

I am trying to set up a event handler for my bot. After I add the code and start the bot it outputs the error:
Error:
Error: The class/prototype returned from the extender function must extend the existing structure class/prototype (received function ExtendedTextChannel extends TextChannel; expected extension of ExtendedTextChannel).
at Function.extend (/home/runner/ricebot/node_modules/discord.js/src/util/Structures.js:82:13)
at module.exports (/home/runner/ricebot/node_modules/discord-buttons/src/index.js:24:16)
at Object.<anonymous> (/home/runner/ricebot/index.js:35:27)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/runner/ricebot/events/ready.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at /home/runner/ricebot/index.js:119:18
Code:
readdirSync("./events/").forEach((file) => {
const events = readdirSync("./events/").filter((file) =>
file.endsWith(".js")
);
for(let file of events) {
let pull = require(`./events/${file}`);
if(pull.name) {
client.events.set(pull.name, pull);
} else {
continue;
}
}
});
The error log leads me to this line 35
require('discord-buttons')(client); and to this line 119 let pull = require(`./events/${file}`)
This really seems confusing to me. Thanks in advance.

Why is console.log showing properties of error?

class AppError extends Error {
constructor(cause, name, ...args) {
const message = args.map(x => x.toString()).join(" ")
super(message)
this.name = name
Error.captureStackTrace(this, AppError)
}
}
throw new AppError(null, "name", "my", "message")
When I throw the error, the output looks like this:
throw new AppError(null, "name", "my", "message")
^
name: my message
at Object.<anonymous> (/home/xxx/test.js:21:7)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11 {
name: 'name'
}
Why is it showing {name: 'name'}?
But when I throw a regular Error object it looks like this:
throw new Error("message")
^
Error: message
at Object.<anonymous> (/home/xxx/test.js:21:7)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
It doesn't show {name: "name or something"}
I am using node ./test.js to run this file.
Your AppError assigned a name property to the instance:
this.name = name
You aren't assigning anything to the plain old Error though. If you do assign such a property, you'll be able to see it:
const err = new Error('errormessage');
err.name = 'someerrorname';
throw err;
results in
someerrorname: errormessage
at Object.<anonymous> (D:\Javascript\...\foo.js:1:13)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
name: 'someerrorname'
}
(Otherwise, what comes before the : will default to Error)
The same sort of thing is true for front-end:
const err = new Error('errormessage');
err.name = 'someerrorname';
throw err;
Uncaught someerrorname: errormessage
(without assigning to err.name, you get Uncaught Error instead)
If you want to remove the {name: 'name'}, make the property non-enumerable:
Object.defineProperty(this, 'name', { value: name, enumerable: false });
When logging an object, enumerable properties will be listed on it.
(You can also omit the enumerable: false if you want, since it defaults to false anyway)

vm.runInContext: Why can't this function access a global object that should be in the context?

Using Node v10.13.0, I don't understand why this code errors out instead of printing "Bill":
const vm = require('vm');
function printName() {
console.log(defaultName);
}
const sandbox = { defaultName: 'Bill', printName: printName };
vm.createContext(sandbox);
vm.runInContext('printName()', sandbox);
When I run it, I get this:
$ node ./eval-js.js
/Users/wiseman/src/typescript-eval-test/eval-js.js:4
console.log(defaultName);
^
ReferenceError: defaultName is not defined
at printName (/Users/wiseman/src/typescript-eval-test/eval-js.js:4:17)
at evalmachine.<anonymous>:1:1
at Script.runInContext (vm.js:107:20)
at Object.runInContext (vm.js:285:6)
at Object.<anonymous> (/Users/wiseman/src/typescript-eval-test/eval-js.js:9:4)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)

TypeError: generateKeyPairSync not a function

I am trying to use the generateKeyPairSync() function of the crypto module of node.js. But am getting a typerError saying that there is no such function.
Its strange because another function of the same module createCipher() works perfectly!
Here is the sample code:
import * as cryp from 'crypto';
export function generate_keys() {
console.log('hey there!');
const keyPair = cryp.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
console.log(keyPair);
}
function encrypt(data: string) {
let cipher = cryp.createCipher('aes-256-ecb', 'pass');
cipher.update(data, 'utf8');
return cipher.final('hex');
}
// This is working
console.log(encrypt('asa'));
// Getting error - 'generateKeyPairSync is not a function'
generate_keys();
This is the complete console output:
[Running] ts-node "d:\projects\myProject\src\ply.ts"
4eb35bc94bd4d17fad4857b17558b2ad
hey there!
d:\projects\myProject\src\ply.ts:5
const keyPair = cryp.generateKeyPairSync('rsa', {
^
TypeError: cryp.generateKeyPairSync is not a function
at generate_keys (d:\projects\myProject\src\ply.ts:5:24)
at Object.<anonymous> (d:\projects\myProject\src\ply.ts:33:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:442:12)
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 Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
[Done] exited with code=1 in 2.097 seconds
From the docs:
Added in: v10.12.0
I guess you are running an earlier version of node

Unable to import Reactjs Class for testing with mocha and enzyme

My test file's code is:
var React = require('react');
var {shalow} = require('enzyme')
var {SearchBox} = require('../static/js/functions')
var expect = require('expect');
describe('Test', function(){
it('1', function(){
expect(true).toEqual(true);
});
});
This is my React class in functions.js
var SearchBox = React.createClass({
render: function() {
return (
<div>
<SearchList data={this.props.data}></SearchList>
<li>
<button id="previous_page" className="previous_page" onClick={back}>Previous</button>
<button id="next_page" className="next_page" onClick={next}>Next</button>
</li>
</div>
)
}
});
It is working fine but I want to write tests and when I run it, it outputs the following error :
irtza#irtza-Lenovo-G50-70:~/Desktop/Kamal Hasan/pedialink$ mocha
./js_test/*.js
/home/irtza/Desktop/Kamal Hasan/pedialink/static/js/functions.js:39
,
^ SyntaxError: Unexpected token <
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:511:25)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object. (/home/irtza/Desktop/Kamal Hasan/pedialink/js_test/test.js:3:19)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at /usr/local/lib/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:485:10)
at Object. (/usr/local/lib/node_modules/mocha/bin/_mocha:405:18)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:159:18)
at node.js:444:3
Your code is a mix between ES5 and ES6 syntax.
I think that the problem come from this portion of code :
var React = require('react');
var {shalow} = require('enzyme');
var {SearchBox} = require('../static/js/functions');
If you wanna use ES5 syntax :
var React = require('react');
var shalow = require('enzyme').shalow; // notice how we access object property `.objectProperty`
var SearchBox = require('../static/js/functions');
If you wanna use ES6 syntax :
import React from 'react';
import {shalow} form 'enzyme'; // notice how we access object property `{objectProperty}`
import SearchBox form '../static/js/functions';
also make sure at the end of your function.js file, you export the component with :
module.exports = SearchBox; // ES5 syntax
export default SearchBox; // ES6 syntax
You need to update Node (mocha is depending on node.js) version (to support es6 modules) or export your component using requireJS - module.exports ... + You should check what parts of es6 your node version supports, because code passed to mocha is not "babelized" :)

Categories

Resources