[英]Writing express JS API with node.JS app error
我正在嘗試在Express.js中創建REST API,但遇到了一些問題, 希望有人可以幫助我 )
我的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 );
});
我的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'
}
]
但是,在選項卡瀏覽器中,我收到此錯誤: "message": "Converting circular structure to JSON"
,但是如果我將.then(res.json(res))
更改為.then(res.json(res))
.then(res.json.bind(res))
,它什么也沒給我,只有清晰的頁面。
因此,我需要在JSON頁面上的REST API中獲取所有這些數據,該怎么辦?
您正在調用res.json
,並將res
傳遞給回調,而不是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);
});
要么
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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.