繁体   English   中英

尝试处理从 NodeJS 中的 API 调用返回的 json 文件 - 错误:未定义 - 为什么?

[英]Trying to process json file returned from an API call in NodeJS - error: undefined - why?

我从 API 调用接收到 JSON 文件(见下文)。 现在,我想访问 JSON 文件的不同键以分别显示各种值。 但是,我在控制台中收到一条“未定义”消息。 有任何想法吗?

这是调用 API 和处理 JSON 文件的代码:

 const input = req.body.var_1; console.log('Request Query:' + JSON.stringify({ "user_input": input})); const articles = []; const sent = 'test'; const api_url = '...'; const options = { method: 'POST', body: JSON.stringify({"user_input": input}), headers: {'Content-Type': 'application/json'} } const response = await fetch(api_url, options); const results = await Promise.all([response.json()]); console.log(results); data = JSON.parse(JSON.stringify(results)) console.log(data); console.log(data.Topic);

这是我得到的控制台 output:

 Request Query:{"user_input":"xgboost"} [ { Topic: { '0': 'random forest, Machine Learning and Wine Quality: Finding a good wine using multiple classifications', '1': 'decision tree learning, Gradient Boost for Classification', '2': 'feature selection, Understanding Multilabel Text Classification and the related process', '3': 'decision tree learning, Gradient Boost for Regression', '4': '<strong class="markup--strong markup--h3-strong">Understanding AdaBoost</strong>, Anyone starting to learn Boosting technique should start first with AdaBoost or…' }, 'URL/PDF': { '0': 'https://dev.to//leading-edje/machine-learning-and-wine-quality-finding-a-good-wine-using-multiple-classifications-4kho', '1': 'https://dev.to//xsabzal/gradient-boost-for-classification-2f15', '2': 'https://dev.to//botreetechnologies/understanding-multilabel-text-classification-and-the-related-process-n66', '3': 'https://dev.to//xsabzal/gradient-boost-for-regression-1e42', '4': 'https://towardsdatascience.com/understanding-adaboost-2f94f22d5bfe' }, Doc_Type: { '0': 'article', '1': 'article', '2': 'article', '3': 'article', '4': 'article' } } ] [ { Topic: { '0': 'random forest, Machine Learning and Wine Quality: Finding a good wine using multiple classifications', '1': 'decision tree learning, Gradient Boost for Classification', '2': 'feature selection, Understanding Multilabel Text Classification and the related process', '3': 'decision tree learning, Gradient Boost for Regression', '4': '<strong class="markup--strong markup--h3-strong">Understanding AdaBoost</strong>, Anyone starting to learn Boosting technique should start first with AdaBoost or…' }, 'URL/PDF': { '0': 'https://dev.to//leading-edje/machine-learning-and-wine-quality-finding-a-good-wine-using-multiple-classifications-4kho', '1': 'https://dev.to//xsabzal/gradient-boost-for-classification-2f15', '2': 'https://dev.to//botreetechnologies/understanding-multilabel-text-classification-and-the-related-process-n66', '3': 'https://dev.to//xsabzal/gradient-boost-for-regression-1e42', '4': 'https://towardsdatascience.com/understanding-adaboost-2f94f22d5bfe' }, Doc_Type: { '0': 'article', '1': 'article', '2': 'article', '3': 'article', '4': 'article' } } ] undefined

很高兴有任何帮助,谢谢

请注意 output 中的[ ] - results (或data ,就此而言)是一个数组,因此Topic将是results[0].Topic (数组本身没有属性Topic ,只有它的元素有一个,因此undefined的结果)。

但是:它甚至不应该是一个数组,它只是一个,因为你在这里做了一个:

const results = await Promise.all([response.json()]);

Promise.all将等待一系列承诺并返回其解析值的数组。 您为数组提供了一个 promise,因此您将返回一个具有一个值的数组,而不是仅获取值本身。

但是使用Promise.all和一个 promise 是没有意义的,因此您可以摆脱该部分,从而也摆脱阵列:

const results = await response.json()
console.log(results.Topic)

(如您所见,整个JSON.parse(JSON.stringify(...))也是多余的。)

看起来您将 Object 嵌套在数组中,因此您必须使用data[0].Topic访问数组中的第一项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM