using split in a module node.js - node.js

var telegram = require('telegram-bot-api');
var api = new telegram({
token: '',
updates: {
enabled: true,
get_interval: 1000
}
});
api.on('message', function(message){
var chat_id = message.chat.id;
var str = message.text;
var word = str.split(" ");
var yr = word[1].split("/");
above is my code, use the telegram-bot-api. Problem is the "split", when the code run, there is error: "TypeError: Cannot read property 'split' of undefined". How can I use split in a module?
thank you

It sounds like either the variable str is undefined or alternatively the value word[1] is undefined. Split works fine in the node environment. I would test it out using some console.logs in the api.on callback. i.e console.log('str>>>', str) console.log('word>>>', word[1])

Related

Koa cannot get the value of the property inside ctx.request-body

Koa cannot get the value of the property inside ctx.request-body
Project koa is generated by koa-generator
Routing section related code
Either require('koa-body') or require('koa-bodyparser')
console.log("ctx")
console.log(ctx.request.body)
console.log(ctx.request.body.type)
})
The three console.log prints are
ctx
{
account:'root',
password: 'test',
type:0
}
undefined
I can get the object inside the ctx.requisition.body and print it out, but ctx.request.body.type is undefined
How to get 'ctx.requisition.body.account' or 'ctx.requisition.body.password' ?
Maybe if you do this
const myObj = JSON.parse(ctx.request.body)
console.log(myObj.type)
You'll get ctx.request.body.type

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}

Reference error! Not defined using node.js / Runkit

I'm trying to use this https://npm.runkit.com/globalpayments-api script but I can't figure what I'm doing wrong.
When I run the Runkit and add the first code to create a new Credit Card it throws error "ReferenceError: CreditCardData is not defined":
const card = new CreditCardData();
card.number = "4111111111111111";
card.expMonth = "12";
card.expYear = "2025";
card.cvn = "123";
How I can point CreditCardData to var globalpaymentsApi = require("globalpayments-api") which contains all this consts?
Demo: https://runkit.com/embed/8hidbubpbk8n
What I'm doing wrong?
Most likely in your code, function CreditCardData() doesn't exist - this means you didn't import it. Try adding this at the beginning of your .js file:
const { CreditCardData } = require('globalpayments-api');

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

Reference undescore in browserify and backbone set up

I am trying to wrap my head around bundling an app as a node module using browserify and I have come across the following scenario:
var
$ = require('jquery')(window),
_ = require('underscore'),
// _ = require('lodash/dist/lodash.underscore'),
Backbone = require('backbone');
Backbone.$ = $;
var TodoView = new Backbone.View.extend({
tagName: 'li',
tpl: _.template('An example template'),
events: {/* dom events */},
render: function() {
this.$el.html(this.tpl(this.model.toJSON()));
return this;
}
});
var todoView = new TodoView();
console.log(todoView.el); // => TypeError: Object [object Object] has no method 'apply'
I can't seem to get a reference for the underscore function. I will definitely be needing it to manipulate data. Templating is just an example use case here as there are other scalable options.
On the same note, I have also tried to reference lodash.underscore to no success and it gives same error.
I have hunch I am missing something here. Any help?

Resources