Readable Stream Node : the _read method with the Number of bytes param - node.js

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.

Related

next() is not a function unable to use generators in NodeJS

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());

NodeJS and PDFkit: 'margin' property gives "Unexpected identifier" error?

I have successfully gotten my code to output a pdf, but when I attempt to adjust the margins using the 'margin' property listed in the documentation using the following code,
var pdf = require ('pdfkit');
var fs = require('fs');
var doc = new pdf(
{
size: [288,144]
}
);
doc.pipe(fs.createWriteStream('run.pdf'));
doc.font('Times-Roman')
.text('Hello different Times Roman!')
doc.addPage({
size: [288,144]
margin : 10
});
doc.end();
I get this error:
margin : 10
^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
Am I missing something obvious here?
Just to ensure any future reader come here with this specific problem, this is a JavaScript Object. When listing JavaScript Objects, every property must be followed by a , except the last instance.
As an example, this:
doc.addPage({
size: [288,144]
margin : 10
});
becomes this:
doc.addPage({
size: [288,144],
margin : 10
});

Main Bower files

In my angularjs app, i try to concat bower file because i have to many lib. I make a task for concat file, here is code
var filter = require('gulp-filter');
var mainBowerFiles = require('gulp-main-bower-files');
var dest = 'dist/scripts';
gulp.task('main-bower-files', function() {
return gulp.src('./bower.json')
.pipe(mainBowerFiles([[filter, ]options][, callback]))
.pipe(gulp.dest('dist/scripts'));
});
After i call this task in nodejs cmd, i get this error in chrome console
.pipe(mainBowerFiles([[filter, ]options][, callback]))
This is error in node cmd
.pipe(mainBowerFiles([[filter, ]options][, callback]))
^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Liftoff.handleArguments (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:198:16)
at module.exports (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
If someone know solution??? Thnx
i fix this with another library, called BOWER-FILES.
var lib = require('bower-files')();
gulp.task('bower_js', function () {
gulp.src(jsBowerFile)
.pipe(concat('bower_js.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(ngmin())
.pipe(uglify({mangle: false}))
.pipe(gulp.dest('dist/scripts'));
});

How to reformat node.js fatal error message to one line?

I want to add nodejs error messages to a nodejs -> file.log -> hekad -> elasticsearch -> kibana stack and I'm facing the issue that heka apparently expects a single error message to be newline terminated, while for fatal errors it has multiple lines, e.g.:
test.js:5
a();
^
ReferenceError: a is not defined
at Object.<anonymous> (test.js:5:1)
at Module._compile (module.js:456:26)
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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
I'm trinking of adding code like to all my scripts:
process.stderr.oldwrite = process.stderr.write;
process.stderr.write = function (m) {
// so that stuff like EventEmitter memory leak gets to 1 line too
process.stderr.oldwrite(m.trim().replace(/\n/g, '\\n') + '\n');
};
process.on('uncaughtException', function(error) {
process.stderr.write(error.stack);
});
This way the output will be on one line clearly meaning it's about a single error, but I'm wondering if it's the correct approach to the problem?

Can't update elements which are inside the array

Let's say I have this
userinfo={
userDetails:
{
username:"",
password:"",
cookie:"",
firstname:"",
lastname:"",
phonenumber:"",
postalcode:"",
country:""
},
applicationsInfo:[
{
application:"",
consumerKey:"",
accessToken:""
}
]
}
First I created user and latter I am to update applicationsInfo section when user creates an application. First I tried this way and It works
var consumerKey="asdyfsatfdtyafydsahyadsy";
var findCon={"userDetails.username":"someName"};
db.find(findCon,function(err,docs){
if(err){
console.log(err);
}else{
var updateCon={$set:{"applicationsInfo.0.consumerKey":consumerKey}};
db.update(findCon,updateCon,{},function(err,docs){
console.log(docs);
});
}
});
But actually what I want is update some selected one I tried that in this way.
........
var appNum=0;
var updateCon={$set:{"applicationsInfo."+appNum+".consumerKey":consumerKey}};
then I start my node server then I got error like this.
/home/jobs/nodeserver/routes/initusers.js:180
"applicationsInfo."+appNum+
^
SyntaxError: Unexpected token +
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)
You need to set it the below way:
var appNum = 0;
var updateCon = {$set:{}};
updateCon.$set["applicationsInfo."+appNum+".consumerKey"] = 1;
Setting an expression ("applicationsInfo."+appNum+".consumerKey") as key of an object during initialization is not allowed in java script.

Resources