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]};
- 将位于/ views文件夹中的index.html设为networking服务器上的login页面
- 如何检索MongoDB中的“相关”文档?
- findOneAndUpdate与returnNewDocument一起使用:true返回原始文档MongoDB
- 查询Mongodb子文档错误将圆形结构转换为JSON
- 我应该如何devise一个WordPress后端+ react + java前端应用程序?
- 如何从NodeJs / express / mongoose中的对象中删除项目?
- AngularJS&Node.js CORS不适用于PUT / POST
- 用户注册为非活动状态,并通过OTP激活
- 刷新路由参数后无法加载索引