0

I'm trying to create a REST API in Express.js but I have some issues, I hope that somebody can help me )

my Express.js code:

  router.get( '/apps/download/:downloadId', function ( req, res, next ) {
    const opts = Object.assign( {downloadId: req.params.downloadId}, req.query );
      gplay.download( opts )
      .then( res.json(res) )
      .catch( next );
  });

My Node.js app jQuery code is:

const data = $( '.row' ).eq( 6 ).find( 'table tr' ).map( function() {
    const a = $( this ).find( 'td:first-child a' );
    const td = $( this ).find( 'td:last-child' );

    return {
        version: a.text(),
        href: a.attr( 'href' ),
        date: td.text()
    }
}).get();

console.log( data )

const sdata = $( '.row' ).eq( 7 ).find( 'table tr' ).map( function() {
    const a = $( this ).find( 'td:first-child a' );
    const td = $( this ).find( 'td:last-child' );

    return {
        version: a.text(),
        href: a.attr( 'href' ),
        date: td.text()
    }
}).get();

console.log( sdata )

So when I open '/apps/download/:downloadId' in the browser it gives me only console.log:

[
]

[
    {
         version: '1.0.2',
         href: '/download-app/com.playgendary.kickthebuddy/5_com.playgendary.kickthebuddy_2018-06-09.apk/',
         date: 'June 9, 2018'
    },

    {
         version: '1.0.1',
         href: '/download-app/com.playgendary.kickthebuddy/4_com.playgendary.kickthebuddy_2018-05-28.apk/',
         date: 'May 28, 2018'
    },

    {
         version: 'Varies with device',
         href: '/download-app/com.playgendary.kickthebuddy/5_com.playgendary.kickthebuddy_2018-05-22.apk/',
         date: 'May 22, 2018'
    }
]

However, in the tab browser I get this error: "message": "Converting circular structure to JSON", but if I change .then(res.json(res)) to .then(res.json.bind(res)), it gives me nothing, just a clear page.

So I need to get all of this data in REST API on the page in JSON, so what should I do?

2
  • res.json(res) Is this intended? You are sending response object as response body. Commented Jun 19, 2018 at 14:20
  • 1
    You are calling res.json with the res passed into the cb, rather than the result of the promise. .then(res.json(res)) should be .then(promiseResult => res.json(promiseResult) Commented Jun 19, 2018 at 14:20

1 Answer 1

1

You are calling res.json with the res passed into the callback, rather than the result of the promise.

router.get('/apps/download/:downloadId', function (req, res, next) {
    const opts = Object.assign({downloadId: req.params.downloadId}, req.query);
      gplay.download(opts)
      .then(downloadResult => res.json(downloadResult))
      .catch(next);
  });

or

router.get('/apps/download/:downloadId', function (req, res, next) {
    const opts = Object.assign({downloadId: req.params.downloadId}, req.query);
      gplay.download(opts)
      .then(res.json)
      .catch(next);
  });
Sign up to request clarification or add additional context in comments.

3 Comments

.then(res.json) gives me clean page. .then(downloadResult => res.json(downloadResult)) gives {"message":"Cannot read property 'app' of undefined"}
console.log still gives me result
thanks for your answer, it help me, i'm finally found reason of this problem !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.