javascript - Promises 链不正确。不返回多个值
问题描述
我想从获取请求中返回两个值:
stats.videoCount
stats.viewCount
使用以下代码,我可以正确返回一个值,但不能同时返回两个值。
以下代码返回视频观看次数。
fetch("https://api.promptapi.com/tiktok/hashtag/planttrees", requestOptions)
.then(response => response.json())
.then(treeViewCountResult => console.log(treeViewCountResult.challengeInfo.stats.viewCount))
.catch(error => console.log('error', error))
但是这段代码不会返回两个值:
.then(response => response.json())
.then(treeVideoResult => console.log(treeVideoResult.challengeInfo.stats.videoCount))
.then(treeViewCountResult => console.log(treeViewCountResult.challengeInfo.stats.viewCount))
.catch(error => console.log('error', error))
treeVideoResult 和 treeViewCountResult 将单独工作,但不能一起工作。
我如何错误地链接承诺?
解决方案
它返回单个对象。那你就不需要两个了。
fetch("https://api.promptapi.com/tiktok/hashtag/planttrees", requestOptions)
.then(response => response.json())
.then(result => console.log(result.challengeInfo.stats.videoCount, resutl.challengeInfo.stats.viewCount))
推荐阅读
- javascript - 如何创建一个使用参数和参数来更改 HTML 标记的字体大小和颜色的 Javascript 函数?
- swift - 具有相同类型“XXXX”作为参数的无效初始化程序调用
- .net - ASP NET CORE 2 部署
- python - Python中字符串和int之间的异或
- python - 将用户名传递给 Django 表单
- vb.net - Click-Once 连接因未指定用户代理而关闭
- protocol-buffers - 在 protobuf v2 中使用地图
- excel - 将报表作为 BMP 从 Excel 复制到 Outlook,并且迷你图不会复制
- reactjs - 使 store 值在 React Redux 组件之外可用
- java - 如何在 Spring 中使用 Apache Commons PropertiesConfiguration 作为 MessageSource?