In Node.js, I am getting an error saying that I have an unexpected identifier for setTimeout(). What's going on? - node.js

As a beginner in programming, I am doing a fun project with a raspberry pi. I am writing a simple program for a raspberry pi with a node module, and I would like to delay the GPIO pins of my pi to turn on and off (for now). I'm using set Timeout() to delay my turning off the GPIO pin.
The big problem is that when I run my file, I get an error saying that setTimeout() is an unexpected identifier. Some help?
More information on the node module that I used can be found in this link: https://www.npmjs.com/package/pi-gpio
My code:
var gpio = require("pi-gpio");
var timers = require("timers"); //thought this might help fix problem
function start(){
gpio.open(7, "output", function(err) { // Open pin 7 for output
gpio.write(7, 1, function() { // Set pin 7 high (1)
gpio.close(7); // Close pin 7
});
});
}
function stop(){
gpio.open(7, "output", function(err) { // Open pin 7 for output
gpio.write(7, 0, function() { // Set pin 7 low(0)
gpio.close(7); // Close pin 7
});
}
setTimeout(start,1000);
setTimeout(stop,3000);
This is the error I got on the SSH terminal:
pi#raspberrypi ~ $ node armrobot.js
/home/pi/armrobot.js:16
setTimeout(start,1000);
^^^^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

You didn't close the stop function correctly.
function stop(){
gpio.open(7, "output", function(err) { // Open pin 7 for output
gpio.write(7, 0, function() { // Set pin 7 low(0)
gpio.close(7); // Close pin 7
});
});
}

Related

Angular giving Syntax Error during initial configuration

I am trying to create an Angular application on a Windows 10 OS. Previously when I attempted to install Node.js and NPM I was getting 'Cannot find 'resolve' of undefined'. I solved it by downgrading my Node.js to 6.17 and the NPM to 3.10. I can now download the Angular CLI but when I use the 'new' command, I get the following error.
C:\Users\user_name\AppData\Roaming\npm\node_modules\#angular\cli\bin\ng:23
);
^
SyntaxError: Unexpected token )
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMain (module.js:611:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:160:9)
When I open the file location I get the following:
#!/usr/bin/env node
'use strict';
// Provide a title to the process in `ps`.
// Due to an obscure Mac bug, do not start this title with any symbol.
try {
process.title = 'ng ' + Array.from(process.argv).slice(2).join(' ');
} catch (_) {
// If an error happened above, use the most basic title.
process.title = 'ng';
}
// This node version check ensures that extremely old versions of node are not used.
// These may not support ES2015 features such as const/let/async/await/etc.
// These would then crash with a hard to diagnose error message.
// tslint:disable-next-line: no-var-keyword
var version = process.versions.node.split('.').map(part => Number(part));
if (version[0] < 10 || version[0] === 11 || (version[0] === 10 && version[1] < 13)) {
process.stderr.write(
'Node.js version ' + process.version + ' detected.\n' +
'The Angular CLI requires a minimum Node.js version of either v10.13 or v12.0.\n\n' +
'Please update your Node.js version or visit https://nodejs.org/ for additional instructions.\n',
);
process.exit(3);
}
require('../lib/init');
It's complaining about the parentheses for the if statement. I tried getting rid of the parentheses and it only makes the situation worse. What would be the best way to go about solving this situation. WOuld it be better to use somehow install a new version of Node and work around the 'resolve' error or take with what I have with the version at hand?

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.

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.

Node's Commander can't prompt terminal

I'm using the popular Commander npm module in a command-line program i'm building. It works great except that all of the functions it provides that solicit user input -- aka, choose, prompt, and password -- fail to work.
As an example I'm using:
program
.command('test')
.action(function(param) {
program.prompt('Username: ', function(name){
console.log('hi %s', name);
});
program.prompt('Description:', function(desc){
console.log('description was "%s"', desc.trim());
});
}
);
This results in the following error (yet it is copy and pasted directly out of the documentation/examples):
TypeError: Object # has no method 'prompt'
at Command. (lib/tg.js:780:11)
at Command.listener (node_modules/commander/index.js:249:8)
at Command.emit (events.js:98:17)
at Command.parseArgs (/node_modules/commander/index.js:480:12)
at Command.parse (/node_modules/commander/index.js:372:21)
at Object. (/lib/tg.js:806:9)
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)
Try using the Node Prompt module. Install it from npm with this command:
npm install prompt --save
The docs can be found here: https://github.com/flatiron/prompt
Make sure you also require it in your code, usually at the top.
var prompt = require('prompt');
Remember, Node is non-blocking. Multiple prompts will attempt to get user input at the same time. To get around this, split your prompts up into functions and call the next prompt in the callback.
Example:
var first_prompt = function() {
var schema = {
// Read the docs on creating a prompt schema
};
prompt.start();
prompt.get(schema, function(err, result) {
// Do stuff with the input
// Call the next prompt
next_prompt();
});
};

limestone module in Nodejs throwing error with nodejs and sphinx

I have been trying to connect sphinx server with nodejs and limestone module. But it is throwing error as follows. Please help me on this.
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: ENOENT, No such file or directory
at doConnect (net.js:549:5)
at Socket.connect (net.js:709:5)
at Object.createConnection (net.js:265:5)
at Object.connect (/home/node/node_modules/limestone/limestone.js:129:23)
at Object.<anonymous> (/home/node/www/bmchat-new/sphinx-connect.js:4:15)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.<anonymous> (module.js:430:10)
Source code of sphinx-connect.js
var limestone = require("limestone").SphinxClient(),
sys = require("sys");
limestone.connect("192.168.2.168:9312", // port. 9312 is standard Sphinx port. also 'host:port' allowed
function(err) { // callback
if (err) {
sys.puts('Connection error: ' + err);
}
sys.puts('Connected, sending query');
limestone.query(
{'query':'test', maxmatches:1},
function(err, answer) {
limestone.disconnect();
sys.puts("Extended search for 'test' yielded " +
answer.match_count + " results: " +
JSON.stringify(answer));
});
});
The issue is that i am using old limestone.js which i have got from npm(v1.0.106). So the updated one, you can get from github
Issue found is, old limestone.js using server_conn = tcp.createConnection(port); instead of the below one server_conn = tcp.createConnection(port, host);

Resources