TypeError:res.json不是一个函数
我试图发送两个JSON,但它不工作。 它打印TypeError: res.json is not a function
但我不明白为什么会发生。 有什么想法吗? 谢谢 !!
app.post('/danger', function response(req, res) { let placeId = req.body.data; let option = { uri: 'https://maps.googleapis.com/maps/api/directions/json?', qs: { origin:`place_id:${placeId[0]}`, destination: `place_id:${placeId[1]}`, language: 'en', mode: 'walking', alternatives: true, key: APIKey } }; rp(option) .then(function(res) { let dangerRate = dangerTest(JSON.parse(res), riskGrid); res.json({ data: [res, dangerRate]}); }) .catch(function(err) { console.error("Failed to get JSON from Google API", err); }) });
因为你正在覆盖你的rp
函数中的res
variables:
app.post('/danger', function response(req, res) { //see, "res" here was being overwritten .. .. rp(option).then(function(response) { //change the variable name of "res" to "response" (or "turtles", who cares, just dont overwrite your up most "res")
.json
不是一个函数。 除非你使用的是一个库,否则JavaScript使用JSON
(使用两个方法.parse()
和.stringify()
,你可以在上面使用这个方法)。
如果你正试图通过.json
的名字设置一个对象属性,那么它将是:
res.json = {data: [res, dangerRate]};