[英]unable to fetch from api
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.listen(3000,function(){
console.log("server is running");
})
app.get("/",function(req,res){
res.sendFile(__dirname + "/index.html");
})
app.post("/",function(req,res){
var url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?";
var pincode = req.body.pinCode;
url = url + "pincode=" + pincode;
var date = req.body.date;
url = url + "&date=" + date;
console.log(pincode,date);
request(url,function(err,res1,body){
res.send(body.centers);
})
})
for the above code (undefined) value is send in res.send(body.centers)
对于上面的代码(未定义)值在res.send(body.centers)
中发送
body is in json format given as below:正文采用 json 格式,如下所示:
{"centers":[{"center_id":596215,"name":"MISSION UHC","address":"MISSION NADIAD","state_name":"Gujarat","district_name":"Kheda","block_name":"Nadiad","pincode":387002,"lat":22,"long":72,"from":"09:00:00","to":"18:00:00","fee_type":"Free"}
Try to see how body looks试着看看身体的样子
request(url,function(err,res1,body) {
console.log(body);
})
If body output in terminal with double quote like: { "key": "value" } that's mean body is JSON string and you need to parsing it to object with:如果终端中的正文 output 带有双引号,例如: { "key": "value" } 这意味着正文是 JSON 字符串,您需要将其解析为 ZA8CFDE6331BD59EB2AC96F8911C4B666
body = JSON.parse(body)
Then send it:然后发送:
res.send(body.centers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.