[英]Unable to send numbers using res.send() using express with node
我試圖在 node 中使用 express 獲得“imdb 評級”,但我正在努力。
電影.json
[{
"id": "3962210",
"order": [4.361276149749756, 1988],
"fields": {
"year": 2015,
"title": "David and Goliath",
"director": "Timothy A. Chey"
},
"doc": {
"_id": "3962210",
"_rev": "1-ac648e016b0def40382d5d1b9ec33661",
"title": "David and Goliath",
"year": 2015,
"rating": "PG",
"runtime": "92 min",
"genre": ["Drama"],
"director": "Timothy A. Chey",
"writer": ["Timothy A. Chey"],
"cast": ["Miles Sloman", "Jerry Sokolosky", "Makenna Guyler", "Paul Hughes"],
"poster": "http://ia.media-imdb.com/images/M/MV5BMjA3OTQ4NDc4MV5BMl5BanBnXkFtZTgwNDYwMzA1MjE@._V1_SX300.jpg",
"imdb": {
"rating": 8.4,
"votes": 138,
"id": "tt3962210"
}
}
}, {
"id": "251656",
"order": [3.489020824432373, 686],
"fields": {
"year": 1999,
"title": "David Cross: The Pride Is Back",
"director": "Troy Miller"
},
"doc": {
"_id": "251656",
"_rev": "1-2d0762776874f94af8f2d76e5991b529",
"title": "David Cross: The Pride Is Back",
"year": 1999,
"rating": null,
"runtime": "55 min",
"genre": ["Comedy"],
"director": "Troy Miller",
"writer": ["David Cross"],
"cast": ["David Cross", "Molly Brenner", "Amiira Ruotola"],
"poster": "http://ia.media-imdb.com/images/M/MV5BODcwMjMxOTU4OF5BMl5BanBnXkFtZTgwODE0MTc4MTE@._V1_SX300.jpg",
"imdb": {
"rating": 7.9,
"votes": 380,
"id": "tt0251656"
}
}
}]
結果
res.send(result.rows[0].doc.imdb);
返回{"rating":8.4,"votes":138,"id":"tt3962210"}
但
res.send(result.rows[0].doc.imdb.rating);
// 不返回 8.4,只是崩潰的節點
和
res.send(result.rows[0].doc.title);
// 返回大衛和歌利亞
res.send(result.rows[0].doc.cast[0]);
// 返回邁爾斯·斯洛曼
我哪里錯了?
body 參數可以是一個 Buffer 對象、一個字符串、一個對象或一個數組
您不能單獨發送號碼。
嘗試將數字轉換為字符串
res.send(''+result.rows[0].doc.imdb.rating);
或將其作為對象值發送
res.send({ result: result.rows[0].doc.imdb.rating});
這有效,您必須將其轉換為字符串:threeFunction().toString() 或 x1.toString()
整數對 res.send() 無效,因此將其轉換為字符串。
res.send(value.toString())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.