Failing to parse results of the nodejs function to specific varaibles - node.js

code...needed to pass the first set of results of the function obj to console. It returns nothing
var gplay = require('google-play-scraper');
var obj = gplay.reviews({appId: 'com'});
var Fieldsomevar = someVar.gplay.reviews[0][0];
console.log(Fieldsomevar);

Related

How do I search an object full of strings in node js?

I'm new to node js programming and I have the following :
var myObj = {
AD: '{+376}',
AF: '{+93}',
AG: '{+1268}'
};
NOTE: I cannot modify this object data, as it comes from a third party component. I have only put an example of what data is returned to me in a local object for debugging purposes.
I'd like to be able to search this object for "AD" and pull out just the +376 from this line
"AD": "{+376}"
this does not seem to work:
var i = myObj.indexOf("AD");
console.log(i);
UPDATE
Sorry... I was using stringify on the object and the output I was seeing in the terminal window was wrong... I have corrected the question
UPDATE again
OK... running it using myObj works in a local sandbox... but using it on the actual data that comes back from the NPM object does not. Here is a RunKit:
https://npm.runkit.com/country-codes-list
This code does returns the number...
var ccl = require("country-codes-list")
var l = ccl.customList('countryCode', '+{countryCallingCode}');
console.log(l.AD);
BUT I need a variable instead of .AD like this:
var ad = 'AD'
var ccl = require("country-codes-list")
var l = ccl.customList('countryCode', '+{countryCallingCode}');
console.log(l.ad); // doesn't work !
This should work.
var ad = 'AD'
var ccl = require("country-codes-list")
var l = ccl.customList('countryCode', '+{countryCallingCode}');
console.log(l[ad]);
You can use the key to reach for the value.
var string = '{"AD":"{+376}","AF":"{+93}","AG":"{+1268}"}';
var object = JSON.parse(string);
function search(id) {
return object[id];
}
console.log(search('AD')) //--> {+376}

Getting Data from a Weather API to a Twitter Bot

I'm setting up a Twitter bot to tweet out a city's temperature, I got the API but I can't seem to hook it on my bot.js file
I tried changing the variables but nothing seems to work.
var Twit = require('twit');
var config = require('./config');
var T = new Twit(config);
gotData();
function setup() {
loadJSON("http://api.apixu.com/v1/current.json?key=7165704df08340e9b00213540192507&q=Colombo", gotData);
}
function gotData(data) {
console.log('Weather Data Retrieved...')
var r = data.current[2];
var tweet = {
status: 'here is ' + r + ' temperature test '
}
T.post('statuses/update', tweet);
}
TypeError: Cannot read property 'current' of undefined
Your call to gotData() does not pass a data argument so when the gotData function attempts to access data.current[2] data is undefined. By the looks of your code you just need to change the line gotData(); to setup();

escodegen.generate throws Error: Unknown node type: undefined

The following is the code that I have written
`js
var esprima = require('esprima');
var escodegen = require('escodegen');
var a = "var a = 2";
var ast = esprima.tokenize(a);
var output = escodegen.generate(ast);
console.log(output);
`
I am able to tokenize the code string but I am getting error generating the code back. I went through multiple samples, Everywhere the same pattern is followed. I don't understand what I am doing wrong.
The function esprima.tokenize does not generate an AST, just an array of tokens. What you want to use is esprima.parse.
Try this:
var esprima = require('esprima');
var escodegen = require('escodegen');
var a = "var a = 2";
var ast = esprima.parse(a);
var output = escodegen.generate(ast);
console.log(output);
It will work

Get new object from another class nodejs

Ok so I have a class that contains
Object JS
var GameServer = require("./GameServer");
var gameServer = new GameServer();
GameServer() contains
GameServer JS
function GameServer() {
// Startup
this.run = true;
this.lastNodeId = 1;
this.lastPlayerId = 1;
this.clients = [];
this.largestClient; // Required for spectators
this.nodes = [];
this.nodesVirus = []; // Virus nodes
this.nodesEjected = []; // Ejected mass nodes
this.nodesPlayer = []; // Nodes controlled by players
}
Now, what im trying to acheive is getting gameServer from ObjectClass
In my class i've tried
new JS
var ObjectClass = require("./ObjectClass");
var gameServer = ObjectClass.gameServer;
But from this way, I won't be able to grab the class GameServer() properties. I'm new to node and im sorry I have to ask this question. I'm currently stuck right now
When I try to grab clients from GameServer
var ObjectClass = require("./ObjectClass");
var gameServer = ObjectClass.gameServer;
gameServer.clients.length;
I get error, clients is undefined. Any way around this?.
I cannot modify GameServer nor Object js.. Basicly im making a script attacthed to a script for extra functionalities.
You are missing the exports of your files so when doing require(file) you're getting and empty object {}..
For gameServer you should be doing something like:
'use strict';
function GameServer() {
// Startup
this.run = true;
this.lastNodeId = 1;
this.lastPlayerId = 1;
this.clients = [];
this.largestClient; // Required for spectators
this.nodes = [];
this.nodesVirus = []; // Virus nodes
this.nodesEjected = []; // Ejected mass nodes
this.nodesPlayer = []; // Nodes controlled by players
}
module.exports = exports = GameServer;
ObjectClass
'use strict';
var GameServer = require("./GameServer");
var gameServer = new GameServer();
exports.gameServer = gameServer;
You need to understand that require cache the value returned by the file, so you would be using a singleton of gameServer.

'Undefined is not a function' in momentjs function DIFF

I have the following code:
var dateFormat = 'YYYY-MM-DD HH:mm:ss';
var time_margin = 10;
var last_message = moment().format(dateFormat);
var comparison = moment(last_message).add(time_margin, 'seconds').format(dateFormat);
var actualtime = moment().format(dateFormat);
var secondsDiff = actualtime.diff(comparison, 'seconds');
console.log("secondsdiff",secondsDiff);
It crashes right in var secondsDiff = actualtime.diff(comparison, 'seconds'); with Missing error handler on "socket".
TypeError: undefined is not a function.
comparison 2015-04-12 18:00:41
actualtime 2015-04-12 18:00:42
What might be wrong? I'm really not understanding
The problem is that you are trying to call diff on a string. When you call moment().format(dateFormat), what you have as result is a string, not an instance of moment.
In order to fix it, you need to call diff without formatting:
var dateFormat = 'YYYY-MM-DD HH:mm:ss';
var time_margin = 10;
var last_message = moment().format(dateFormat);
var comparison = moment(last_message).add(time_margin, 'seconds').format(dateFormat);
var secondsDiff = moment().diff(comparison, 'seconds');
console.log("secondsdiff",secondsDiff);
// => secondsdiff -9

Resources