Javascript 编写带有node.JS应用程序的express JS API时出错

Javascript 编写带有node.JS应用程序的express JS API时出错,javascript,json,node.js,api,express,Javascript,Json,Node.js,Api,Express,我正在尝试在Express.js中创建REST API,但我有一些问题,希望有人能帮助我 my Express.js代码: router.get( '/apps/download/:downloadId', function ( req, res, next ) { const opts = Object.assign( {downloadId: req.params.downloadId}, req.query ); gplay.download( opts )

我正在尝试在Express.js中创建REST API,但我有一些问题,希望有人能帮助我

my Express.js代码:

  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应用程序jQuery代码为:

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 )
因此,当我在浏览器中打开“/apps/download/:downloadId”时,它只会给我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'
    }
]
然而,在选项卡浏览器中,我得到了以下错误:消息:将循环结构转换为JSON,但是如果我将.thenres.jsonres更改为.thenres.JSON.bindres,它不会给我任何东西,只会给我一个清晰的页面

因此,我需要在页面上的REST API中以JSON格式获取所有这些数据,那么我应该怎么做呢?

您调用res.JSON时将res传递到回调中,而不是承诺的结果

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


这是有意的吗?您正在将响应对象作为响应体发送。您正在调用res.json,并将res传递到cb中,而不是承诺的结果。thenres.jsonres应该是。ThenPromiseSult=>res.jsonPromiseSult.thenres.json为我提供了干净的页面。thendownloadResult=>res.jsondownloadResult提供{消息:无法读取未定义的}console.log的属性'app',仍然提供结果感谢您的回答,这对我有帮助,我终于找到了这个问题的原因!
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);
  });