javascript - 无法控制台记录在另一个项目中有效的 API 响应
问题描述
基本上,我想调用加密数据(开盘价、最高价、最低价、收盘价)并将其输入 tradeview 图表库并显示图表。
按照 tradingview 教程,我尝试console.log
将控制台上的 API 结果作为第一步。
到目前为止,我已经对此进行了编码
helpers.js
export async function makeApiRequest() {
console.log("API FUNCTION")
try {
const query = `
{
ethereum(network: bsc){
dexTrades(options: {limit: 100, asc: "timeInterval.minute"},
date: {since:"2020-11-01"}
exchangeName: {is: "Pancake"},
baseCurrency: {is: "0xbA2aE424d960c26247Dd6c32edC70B295c744C43"},
quoteCurrency: {is: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"}){
timeInterval {
minute(count: 1)
}
baseCurrency {
symbol
address
}
baseAmount
quoteCurrency {
symbol
address
}
quoteAmount
trades: count
quotePrice
maximum_price: quotePrice(calculate: maximum)
minimum_price: quotePrice(calculate: minimum)
open_price: minimum(of: block get: quote_price)
close_price: maximum(of: block get: quote_price)
}}
}`;
res = await fetch('https://graphql.bitquery.io/', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json; charset=utf-8',
'X-API-KEY': 'xxxxxxxxxxxxxxxx',
}),
body: JSON.stringify({ query }),
mode: 'cors',
});
console.log(res)
return res;
} catch (error) {
throw new Error(`API error: ${error.status}`);
}
}
这个函数在另一个像这样调用的文件中被调用datafeed.js
。
import {
makeApiRequest,
generateSymbol,
parseFullSymbol,
} from './helpers.js';
const lastBarsCache = new Map();
const configurationData = {
supported_resolutions: ['1D', '5', '1W'],
exchanges: [{
value: 'Bitfinex',
name: 'Bitfinex',
desc: 'Bitfinex',
},
{
value: 'Kraken',
name: 'Kraken',
desc: 'Kraken bitcoin exchange',
},
// {
// value: 'PancakeSwap',
// name: 'PancakeSwap',
// desc: 'PancakeSwap',
// },
],
symbols_types: [{
name: 'crypto',
value: 'crypto',
}],
};
async function getAllSymbols() {
const data = await makeApiRequest();
console.log("/////////////"+data)
return data;
}
export default {
onReady: (callback) => {
console.log('[onReady]: Method call');
console.log(getAllSymbols())
setTimeout(() => callback(configurationData));
},
searchSymbols: async (
userInput,
exchange,
symbolType,
onResultReadyCallback,
) => {
console.log('[searchSymbols]: Method call');
const symbols = await getAllSymbols();
console.log(symbols)
};
这给出了以下错误
Uncaught (in promise) Error: API error: undefined
at makeApiRequest (helpers.js:48)
at async getAllSymbols (datafeed.js:35)
但是,如果您在bitquery 在线 IDE上测试 api 请求- 它可以工作
{
ethereum(network: bsc){
dexTrades(options: {limit: 100, asc: "timeInterval.minute"},
date: {since:"2020-11-01"}
exchangeName: {is: "Pancake"},
baseCurrency: {is: "0xbA2aE424d960c26247Dd6c32edC70B295c744C43"},
quoteCurrency: {is: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"}){
timeInterval {
minute(count: 1)
}
baseCurrency {
symbol
address
}
baseAmount
quoteCurrency {
symbol
address
}
quoteAmount
trades: count
quotePrice
maximum_price: quotePrice(calculate: maximum)
minimum_price: quotePrice(calculate: minimum)
open_price: minimum(of: block get: quote_price)
close_price: maximum(of: block get: quote_price)
}}
}
我在这里犯了什么错误?如何console.log
正确获取数据?
更新:我可以在网络选项卡中看到正在获取的数据,但它仍然没有在控制台打印。
浏览器控制台中的错误:
解决方案
在编码之前使用 Postman 测试 QUERY(为什么命名响应?)!确保正确定义了所有标题/元素,并且它是否按预期工作。
命名 const 是有原因的query
- graphql 请求需要字段/参数'query'
[和可选'variables'
]
片段 ...body: JSON.stringify({ response }),
是body: JSON.stringify({ response: response }),
while的快捷方式body: JSON.stringify({ query: response }),
是必需的。将 [您的查询] 重命名'response'
为'query'
可以将其简化为body: JSON.stringify({ query }),
.
推荐阅读
- python - 从荷兰名称中获取前缀
- docker - 当服务版本更改而不是底层哈希时,我可以避免在 docker-compose up 上重新启动服务吗?
- ros - 为什么机器人在 RVIZ 中不动?
- firebase - Firebase 托管:标头未生效(COEP 和 COOP http 标头)
- azure-pipelines - Azure Devops 代理无法运行作业
- dynamic-programming - 返回方程的非负整数解的数量
- elasticsearch - 避免在 Confluent Elasticsearch 连接器中为空值覆盖字段
- firebase - 如何将 Firebase Web 应用程序与 fcm 连接?
- reactjs - 如何确定 FlatList 何时完成显示数据?
- python - Django Admin中的多个外键条目