next() is not a function unable to use generators in NodeJS - node.js

I am using the following code in NodeJS 7.10.0:
function * gen(){
yield 100;
yield 200;
yield 300
}
console.log(gen.next());
and I get this in return:
TypeError: gen.next is not a function
at Object.<anonymous> (/Files/development/learning/node-bits/generators.js:15:17)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

When you call a generator function, its body is not executed. Instead it returns you an iterator object. When you call iterator.next(), body is executed, you get the value and context is maintained.
You can do
let i= gen()
console.log(i.next())
and you will get the desired behaviour.
Refer this:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function*

You should assign the output of gen to a variable and call next on the returned value:
const out = gen();
console.log(out.next());
console.log(out.next());
console.log(out.next());
console.log(out.next());

Related

How to rectify this error "TypeError: EthereumTransaction is not a constructor"?

How do I rectify this type error? This is the code for making a transaction and the error received at the terminal is also added.
var transaction = new EthereumTransaction(rawTransaction)
^TypeError: EthereumTransaction is not a constructor
Here is my code:
var privateKeySender = '*************************************'
var privateKeySenderHex = new Buffer(privateKeySender, 'hex')
var transaction = new EthereumTransaction(rawTransaction)
transaction.sign(privateKeySenderHex)
$ node index.js
C:\Users\asus\Desktop\projectFolder\index.js:43
var transaction = new EthereumTransaction(rawTransaction)
^
TypeError: EthereumTransaction is not a constructor
at Object.<anonymous> (C:\Users\asus\Desktop\projectFolder\index.js:43:19)
at Module._compile (internal/modules/cjs/loader.js:778:30)
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 Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
The Ethereumjs api has been updated. Do the following instead
var privateKeySender = '*************************************'
var privateKeySenderHex = new Buffer(privateKeySender, 'hex')
var transaction = new EthereumTransaction.Transaction(rawTransaction)
transaction.sign(privateKeySenderHex)

Getting error while using "fs" module to read file in node.js

I am trying to read file using "fs" module in node.js as follows:
var fs=require("fs");
fs.read("E:/Node.js/readme.txt","utf8",function(err,data){
console.log(data);
});
But getting the following error:
fs.js:664
binding.read(fd, buffer, offset, length, position, req);
^
TypeError: fd must be a file descriptor
at Object.fs.read (fs.js:664:11)
at Object.<anonymous> (E:\Node.js\First.js:2:4)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
Why is it so?
You should use readFile instead of read, read use for partial read bytes from file
fs.readFile("E:\\Node.js\\readme.txt",'utf8', function(err,data){
console.log(data);
});

Readable Stream Node : the _read method with the Number of bytes param

I am trying to figure out streams in node and playing around with some examples in the stream handbook
I am trying out the _read method of a readable stream. It says, it takes in a parameter which is the number of bytes the consumer wants to read.
I have two questions here.
Is the number of bytes the consumer wants to read the 'watermark'
Why do i get an error when I use _read with a parameter.
This is my code.
var Readable = require('stream').Readable;
var rs = Readable();
var c = 97;
rs._read = function (5) {
rs.push(String.fromCharCode(c++));
if (c > 'z'.charCodeAt(0)) {
rs.push('\n');
rs.push(null);
}
};
setTimeout(function () {
rs.pipe(process.stdout);
}, 2000);
And this is the error
/Users/nikhilkuria/Dev/git/node_demo/streams/streamRead.js:5
rs._read = function (5) {
^
SyntaxError: Unexpected number
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
function (5) { is not valid syntax for creating a function. Instead of a number, you'll want to pass in a variable name, like this: function (a) {
The error you're seeing is due to JavaScript not allowing variable names starting with or consisting of only numbers.

Function and module.exports error issue NODE.js

:)
I have a somewhat easy answer for you guys to answer as you always do.
Im new at functions and whatnot, iv watched some tutorials about exporting your functions to another node.js application,
Im attempting to generate some random numbers for a external module.
this is what i have setup.
(index.js file)
function randNumb(topnumber) {
var randnumber=Math.floor(Math.random()*topnumber)
}
module.exports.randNumb();
(run.js)
var index = require("./run.js");
console.log(randnumber);
Well My Issue is when i run the index.js file, i get this error from the console.
TypeError: Object #<Object> has no method 'randNumb'
at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
s - Random Number\index.js:8:16)
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.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
I ran the run.js in the beginning, this is what i got.
ReferenceError: randNumb is not defined
at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
s - Random Number\run.js:3:1)
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.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
In the randNum function, don't forget to:
return randnumber;
Also in index.js, export the function like so:
exports.randNumb = randNumb;
Invoke it like this in run.js:
console.log(randNumber(10));
You didn't export the var at all.
Your index.js should be :
function randNumb(topnumber) {
return Math.floor(Math.random()*topnumber)
}
module.exports.randnumber = randNumb(10); //replace 10 with any other number...
run.js should be :
var index = require("./run.js");
console.log(index.randnumber);

TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'mapDefaultAction' with nodejs

I was getting error with express-resource so I have applied patch as mentioned in following link.
As mentioned in following question at stack overflow:
express-namespace and express-resource wont allow map
I employed following command:
npm install git://github.com/visionmedia/express-resource.git#add/express3x-support
but now I am getting following error:
C:\Data\work\node\example\www\example\node_modules\express-resource\index.js:63
if (actions[key]) this.mapDefaultAction(key, actions[key]);
TypeError: Object function app(req, res){ app.handle(req, res); } has no method
'mapDefaultAction'
at Function.Resource (C:\Data\work\node\example\www\example\node_modules\
express-resource\index.js:63:28)
at Object.<anonymous> (C:\Data\work\node\example\www\mixerade\app.js:23:17)
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.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
can anyone please suggest what could be the possible reason?
Thanks in advance.

Resources