NodeJS: Async / Await Raspberry Pi - node.js

I have some code that works perfectly on my day-to-day computer but that gives me an error when trying to launch from a raspberry pi (3 model B). The error goes like:
> setInterval(async function () {
> ^^^^^
>
> SyntaxError: missing ) after argument list
> at exports.runInThisContext (vm.js:53:16)
> at Module._compile (module.js:414:25)
> at Object.Module._extensions..js (module.js:442:10)
> at Module.load (module.js:356:32)
> at Function.Module._load (module.js:311:12)
> at Function.Module.runMain (module.js:467:10)
> at startup (node.js:134:18)
> at node.js:961:3
And my code is a 'simple' setInterval (1500 ms) with a async / await in it:
setInterval(async function () {
var data = await foo();
var obj = new mongooseModel({
mk: data.mk,
name: data.name,
a: data.a,
b: data.b,
c: data.c,
v: data.v,
p: data.p,
l: data.l,
h: data.h,
o: data.o,
sn: data.sn,
n: data.n,
});
obj.save(function(err, tick) {
if (err) return console.log(err);
});
});
}, 1500);
anyone else has ever had this problem?
Thanks in advance!!

async/await supported by node.js starting from v7.6. You need to upgrade installed node.js version or use Promises.

Related

"unexpected token function" error for async, even though node version is 7.6.0+

I'm running out of ideas. I was running node 6.5.0, but then I needed to write some code that has async/await. So I updated node version to 7.7.0 (verified by running node -v) and tried running my app.js again. Unfortunately I'm getting the same error.
Do you have any idea what is happening? I'm running the app with a command node app.js and node -v gives me v7.7.0 response.
I hope I included enough information, please let me know if there's anything I could add to help diagnose this.
Error:
async function navigateWebsite() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:528:28)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/lucas/app/myapp/app.js:178:19)
at Module._compile (module.js:556:32)`
I'm pasting this function into my app.js application - not sure how code helps if it's clearly an issue with node and not the code.
function test() {
return new Promise(resolve => {
setTimeout(() => {
resolve('test');
}, 2000);
});
}
async function navigateWebsite() {
const msg = await test();
console.log('Message:', msg);
}

Vuex without Vue?

I'd like to use Vuex to power a server-side application that doesn't use Vue. Is this possible?
const Vuex = require('vuex');
const store = new Vuex.Store({
state: {
potatoes: 1,
},
getters: {
doublePotatoes(state) {
return state.potatoes * 2;
},
},
mutations: {
addPotato(state) {
state.potatoes += 1;
},
}
});
store.watch((state, getters) => getters.doublePotatoes, console.log);
store.commit("addPotato");
Here's the error I get:
$ node index.js
/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:99
if (!condition) { throw new Error(("[vuex] " + msg)) }
^
Error: [vuex] must call Vue.use(Vuex) before creating a store instance.
at assert (/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:99:27)
at new Store (/private/tmp/vtest/node_modules/vuex/dist/vuex.common.js:279:5)
at Object.<anonymous> (/private/tmp/vtest/index.js:3:15)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
I wound up adding Vue without creating a Vue app:
const Vue = require('vue');
Vue.use(Vuex);
My tiny test store works now. I don't know if Vue.use(Vuex) without creating a Vue app will cause any problems.
Of note for server-side use cases, Vuex doesn't call my watcher for every commit; it batches changes and calls the watcher once. I don't see this documented.

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.

Any idea what i'm doing wrong? NODEJS

...
if (response.summ > check) {
io.emit('acceptoffer', {steamid: offer.steamid_other})
helper.msg('More Than Min - ' + offer.tradeofferid);
if(timer <= 28 && timer != 0) {
offers.declineOffer({tradeOfferId: offer.tradeofferid}, function() {
currentGameOffers.splice(currentGameOffers.indexOf(offer.tradeofferid), 1);
helper.msg('Timer less than 28 seconds - ' + offer.tradeofferid);
} else {
try {
offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(err, log) {
if (err) {
...
Any idea what i'm doing wrong over here?
the error i get is that
} else {
^^^^
SyntaxError: Unexpected token else
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
What i'm trying to do is if the timer on the javascript is <=28 and <> from 0 then do offer.declineOffer and if its not run the offer.AcceptOffer.
I'm really sorry if i didnt supose to post it here and i'm also sorry for my english.
You have an open ( from offers.declineOffer and an open { from your if block. You should format your code to see the problem:
if (response.summ > check) {
io.emit('acceptoffer', {steamid: offer.steamid_other})
helper.msg('More Than Min - ' + offer.tradeofferid);
if (timer <= 28 && timer != 0) {
offers.declineOffer({tradeOfferId: offer.tradeofferid}, function () { // starts a function definition
currentGameOffers.splice(currentGameOffers.indexOf(offer.tradeofferid), 1);
helper.msg('Timer less than 28 seconds - ' + offer.tradeofferid);
} // closes the function definition
// OOPS! There's no } to close the if block.
// You also need to close the ( from offers.declineOffer
// Add the ) and } here, like this:
);
}
else
{
try {
offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function (err, log) {
if (err) {
First thing first, rephrase your question before it gets too many downvotes and gets buried under, umm, something. Proper sentences are required to understand the question and intent of question, so that others can help you.
You have added else to closing brace for the function.
Instead try:
...
})
} else {
...
HTH

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