gpt4 book ai didi

javascript - 无法使用 res.send() 使用 express with node 发送数字

转载 作者:数据小太阳 更新时间:2023-10-29 05:07:16 25 4
gpt4 key购买 nike

我试图在 Node 中使用 express 获得“imdb 评级”,但我很挣扎。

movies.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,只是崩溃的 Node

res.send(result.rows[0].doc.title);//返回大卫和歌利亚

res.send(result.rows[0].doc.cast[0]);//返回 Miles Sloman

我哪里错了?

最佳答案

根据 Express res.send([body])文档:

The body parameter can be a Buffer object, a String, an object, or an Array

您不能单独发送号码。

尝试将数字转换为字符串

res.send(''+result.rows[0].doc.imdb.rating);

或将其作为对象值发送

res.send({ result: result.rows[0].doc.imdb.rating});

关于javascript - 无法使用 res.send() 使用 express with node 发送数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39498601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com