skip to Main Content

I am extract fields from a json object but for some reason I keep getting undefined. I have an array inside the json array “nightclub”. I want to extract the data from that field and put it inside an array. How can I go about it? This is what I tried so far. I am not using the express framework and I am bit concerned about how I am parsing the json

var TelegramBot = require('node-telegram-bot-api');
var request = require('request');
var http = require('http');
var express = require("express");
var app = express();

var fd = require("./Services.js");
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
const TOKEN = process.env.TELEGRAM_TOKEN || '****************';
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('Hello, world! [helloworld sample;');
}).listen(3009);

telegram = new TelegramBot(TOKEN, { polling: true });
telegram.on("text",function (message,req,res) {

    var messagetext = message.text;
    var receiver = message.chat.id; //the user receiving the response from the bot
    var timestamp = message.date; //timestamp
    var msgid = message.message_id;//message id
    var sender = message.from.id; //id of the telegram bot
    console.log("message",messagetext);
    switch (messagetext) {
        case "Menu":
            telegram.sendMessage(sender,"Enter the name of the menu that you would like to choose");
            fd.itemcategory().then(function (v) {
                console.log(v);
                for (var i = 0; i < 5; i++) {
                    telegram.sendMessage(sender, v[i]);
                }
            });
            break;

        case "Nightclub":
            telegram.sendMessage(sender,"Here are some of the best nightlife in Nairobi");
            fd.categorydata("Nightclub").then(function(v){
              var obj = v;


                console.log("entered",obj.info);




            });
            break;



    }









});







This is the response I am receiving from the api
    {
        "info": {
            "response": "sucess",
            "nightclub": [
                "fjfjkfkf",
                "jfkfkfkf",
                "fujfjkfkf",
                "fjfjfklf",
                "fjfjfkkf"
            ]
        }
    }

This is what I get in my console –

entered undefined

2

Answers


  1. you would have to use the variable

    v.info.nightclub
    
    let obj = {
        "info": {
            "response": "sucess",
            "nightclub": [
                "fjfjkfkf",
                "jfkfkfkf",
                "fujfjkfkf",
                "fjfjfklf",
                "fjfjfkkf"
            ]
        }
    }
    
    console.log(obj.info.nightclub);

    From the comments it is shown that you were getting a string as an input so just use

    obj = JSON.parse(obj)
    

    to convert it into a valid json object

    Login or Signup to reply.
  2. To get it in an array, you could run it through a for each (or jquery $.each) where you are pointing at v.info.nightclub.

    Something like this

    var nightclubs = []
    $.each(v.info, function(index, entry{ 
    nightclubs.push(entry.info.nightclub);
    }
    console.log(nightclubs)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search