[英]Node.js changing type of variable from Object to Undefined
I am trying to make API call from Node.js Expressjs.我正在尝试从 Node.js Expressjs 进行 API 调用。 API call is working fine however after parsing the response to JSON, when I try to access different fields it gives me an error. API 调用工作正常,但是在解析对 JSON 的响应后,当我尝试访问不同的字段时,它给了我一个错误。 I try to log the type of one element of the JSON object to console.我尝试将 JSON 对象的一个元素的类型记录到控制台。 First it says Object and then it says undefines (or if try to log the content itself, it gives me an error).首先它说 Object 然后它说 undefines (或者如果尝试记录内容本身,它会给我一个错误)。
var request = require('request');
const app = express();
//For serving static pages
// app.use(express.static('./static'))
app.get('/', (req, res) =>{
res.send('You have successfully contacted the API')
});
app.get('/:ticker', (req, res) => {
var requestOptions = {
'url': 'https://api.tiingo.com/tiingo/daily/' + req.params.ticker + '/prices?startDate=2019-01-02&token=74e7c9d22c2ffe8e9b5643edc2ef48bbddc6e69e',
'headers': {
'Content-Type': 'application/json'
}
};
request(requestOptions,
function(error, response, body) {
result = JSON.parse(body);
console.log('The type is ' + typeof (result[0].date)) //This statement prints different results twice
res.send(result);
}
);
});
app.listen(process.env.PORT || 3000)
Terminal says:终端说:
The type is string
D:\Web Dev Bootcamp\NodeJS-Tutorial\index.js:24
console.log('The type is ' + typeof (result[0].date))
^
TypeError: Cannot read property 'date' of undefined
at Request._callback (D:\Web Dev Bootcamp\NodeJS-Tutorial\index.js:24:60)
at Request.self.callback (D:\Web Dev Bootcamp\NodeJS-Tutorial\node_modules\request\request.js:185:22)
at Request.emit (events.js:314:20)
at Request.<anonymous> (D:\Web Dev Bootcamp\NodeJS-Tutorial\node_modules\request\request.js:1154:10)
at Request.emit (events.js:314:20)
at IncomingMessage.<anonymous> (D:\Web Dev Bootcamp\NodeJS-Tutorial\node_modules\request\request.js:1076:12)
at Object.onceWrapper (events.js:420:28)
at IncomingMessage.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1223:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) [nodemon] app crashed - waiting for file changes before starting...
Run a quick console.log(result) to make sure that result is being stored as an Array.运行一个快速的 console.log(result) 以确保结果被存储为一个数组。 I suspect that when you are parsing the JSON it is storing result as an object.我怀疑当您解析 JSON 时,它会将结果存储为对象。
Figured out what the issue was.弄清楚是什么问题了。 Actually express was making a request for favicon.ico by itself and during that request apparently result variable was undefined.实际上 express 是自己请求 favicon.ico 并且在该请求期间显然结果变量未定义。 I simply set up another route for handling favicon request as suggested [here][1].我只是按照 [此处][1] 的建议设置了另一条路线来处理网站图标请求。 Now the code is simply logging the result once and I am able to access the fields of JSON object properly.现在代码只是简单地记录一次结果,我可以正确访问 JSON 对象的字段。
Edit: as pointed out by a contributor, its the browser thats making the favicon request and 'not' the express.编辑:正如一位贡献者所指出的,它的浏览器正在发出收藏夹图标请求,而不是“快递”。 [1]: Node Express "favicon.ico" not found error [1]: Node Express "favicon.ico" not found 错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.