-3

enter code hereMy promise statement here is returning results as undefined. How can I access the data from promise and use it? here are my code. This will return and display the real-time schedule, but I just can't access the data and use them. Please help me with accessing the data. Thanks!

       var async = require("async");
    var fetch = require('node-fetch');
    var Mta = require('mta-gtfs');
    var mta = new Mta({
            key:'my_api',
            feed_id: 1
    });

    fetch('http://datamine.mta.info/lists-of-feeds')


    //********************************************************************************************
    //--------------------------------------------------------------------------------------------
    //mta.stop('627').then(function (result) {
    //       console.log(result);
    //    });


    //--------------------------------------------------------------------------------------------
    /*
     function getStopInfo(StopId){
             mta.schedule(StopId).then(function (result) {
                    console.log('*****************************************')
            for (var i=0; i<5;i++)
            {
                    console.log('train '+[i+1]+ ' heading to North: '+result.schedule['627'].N[i]['arrivalTime']);
                    console.log('train '+[i+1]+ ' heading to South: '+result.schedule['627'].S[i]['arrivalTime']);
                    console.log('*****************************************')
            }


    });
    }

    function getInfo(StopId, callback){
            if (typeof callback == 'function')
            {
            console.log('works')
            callback(StopId);
            }
            else
            {
            console.log('err');
            }
    }





 //console.dir(result, {depth:null, color:true});
    //const info = getStopInfo(627);
    //console.log(info);

    var info = getInfo(627, getStopInfo);
    console.log(info);
    */

   async function getStopInfo(StopId){   //This is where the function is being pointed out for unexcepted token.
            try{
            const info = await mta.schedule(StopId);
            callback(info)
            } catch(error) {
            console.log(error)
            }
    }

getStopInfo(627, info => {
        console.log('yep' +info)
})
Sen Cai
  • 1
  • 3
  • 4
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mark Aug 08 '18 at 16:06

2 Answers2

0

info will be undefined, as you should add return into getStopInfo function.

You function should be

function getStopInfo(StopId){
    return mta.schedule(StopId).then(function (result) {
        console.dir(result, {depth:null, colors:true});
        return result;
    });
}
Alex Ivasyuv
  • 8,585
  • 17
  • 72
  • 90
  • If the OP uses this as they do in the question `const info = getStopInfo(627);` do you think `info` will be what they are expecting? – Mark Aug 08 '18 at 16:12
  • @MarkMeyer Who knows what they are expecting? It doesn't return undefined any more, and the function is fine now. – Bergi Aug 08 '18 at 16:17
  • Thank you for replaying. I tried to remove return and everything become undefined, and if I add return in both place, the result is still undefined. This function along with display the real-time data from subway system. I want to access the data which I get from this function. This function is able to display the data but I can't access the data. What should I do to access the data ? – Sen Cai Aug 09 '18 at 12:16
  • You can use a callback to return it out of the function... let me revise my answer. – jremi Aug 09 '18 at 14:59
0

Since I dont know the exact api you are using and how exactly the mta.schedule returns the promise data... I have stubbed it out in this example... Basically I think you will possibly want to consider using a async/await function setup ... Check out this idea here...

let Mta = require('mta-gtfs');
let mta = new Mta({
  key: 'MY-MTA-API-KEY-HERE',
});

async function getStopInfo(StopId, callback){
    try {
        const info = mta.schedule(StopId)
        callback(info)
    } catch(error){
        console.log(error)
    }
}

getStopInfo(627, info => {
  console.log('yep info is outside now', info)
})
jremi
  • 2,879
  • 3
  • 26
  • 33
  • Thanks for your replying. I'm using mta-gtfs api. I tried your code. the system returned Unexpected token new which is the new in third line. What should I do with this ? – Sen Cai Aug 09 '18 at 14:47
  • in my example... the mta object is not what you will use in your code... This is just stubbed out for the example... However, in my example the only use of the 'new' keyword is for creating the promise However, i think your mta-gtfs api is already returning you a promise... – jremi Aug 09 '18 at 14:49
  • I just looked at the npm mta-gtfs which returns promises... so here is an example i am revising my answer... – jremi Aug 09 '18 at 14:57
  • Yeah. mta-gtfs is returning the promise to me and I can see it, but the I cannot access the data and use the data. What should I do in order to access and use the data outside the promise ? Thank you for replaying. – Sen Cai Aug 09 '18 at 14:59
  • see my last revision using a callback to extract the info resolved promise from the async function and then using the callback to put the result out. – jremi Aug 09 '18 at 15:03
  • The system says unexpected token function which points to the function that after async. What shoud I do ? – Sen Cai Aug 09 '18 at 15:07
  • Please provide a jsbin or jsfiddle so we can see your code. – jremi Aug 09 '18 at 15:08
  • I have updated my question. The code is at top. Please check it out. Thank you for the help. – Sen Cai Aug 09 '18 at 15:15
  • I see your problem... you forgot to put the callback function parameter into your function... its missing – jremi Aug 09 '18 at 15:19
  • Your example has.... function getStopInfo(StopId){}.... You need to have ... function getStopInfo(StopId, callback){} ..... You need to put callback in the function paramters so it can actually send back the result. – jremi Aug 09 '18 at 15:19
  • Sorry for the confusion. The code I made is not working when I try to print the result, console showed undefined result. I have update the code of mine on the top and I point out the place where the error comes out. Please check it out. – Sen Cai Aug 09 '18 at 15:23
  • I did... you are mssing the callback – jremi Aug 09 '18 at 15:23
  • The example you put is not matching what I put... you need to put.... function getStopInfo(StopId, callback) .... – jremi Aug 09 '18 at 15:25
  • function getStopInfo(StopId, callback){ mta.schedule(StopId).then(function (result) { console.log('*****************************************') for (var i=0; i<5;i++) { console.log('train '+[i+1]+ ' heading to North: '+result.schedule['627'].N[i]['arrivalTime']); console.log('train '+[i+1]+ ' heading to South: '+result.schedule['627'].S[i]['arrivalTime']); console.log('*****************************************') } Where else should I add the callback. Sorry for so many questions, I'm a novice in node.js }); } – Sen Cai Aug 09 '18 at 15:26
  • It's ok. I have find it out. as long as I know that I need to use callback. Thank you sooooo much for spending your time. – Sen Cai Aug 09 '18 at 15:32
  • If you paste your code in jsfiddle and share link we can see better – jremi Aug 09 '18 at 15:33
  • i dont really understand your example... please just tell me exactly what you want to see happen with your code? – jremi Aug 09 '18 at 15:43
  • Sorry for the confusion. I been trying this for days and I have comment out a lot of codes just for reference. I want to pull the real-time schedule from mta system and use the data for display purpose. Now I can see the data from their system by using this code, but I cannot access the data to filter the useful information. This is where I got stack, I cannot access data from outside the promise. – Sen Cai Aug 09 '18 at 15:46
  • Check this fiddle ... super simple without all of the extra stuff, just very basic usage of the promise from within your function and then having the result of the promise send back to the callback for using "outside the promise" as you say. https://jsfiddle.net/57a8n6de/ – jremi Aug 09 '18 at 15:48
  • I tried your code, I add var info = getStopInfo(627, function(error, result) and console.log(info), the system still shows undefined. – Sen Cai Aug 09 '18 at 15:59