Node Discord Bot股票报价代码语法错误排查问询
Issues & Fixes for Your Discord Bot Stock Quote Code
Hey there, let's walk through the problems in your code that are stopping it from pulling stock symbol, name, and price correctly:
- Broken arrow function syntax: You've got
async () =>in your code — that's an HTML escaped character instead of the standard=>arrow function syntax. JavaScript won't recognize this, so it's a direct syntax error that breaks the function. - Incorrect handling of API response: The Financial Modeling Prep
/quoteendpoint returns an array of objects (even when you request a single stock like AAPL). Your current code tries to accessquoteValue.symboldirectly, butquoteValueis an array, not a single object. You need to grab the first element from the array. - Redundant variable name (optional clean-up): You declared a function named
getQuoteinside thecommand === 'getQuote'block. While this isn't a syntax error, it's confusing to reuse the same name like this — renaming the function to something likefetchStockQuotemakes the code clearer.
Fixed Code
if (command === 'getQuote') { const fetchStockQuote = async () => { const response = await axios.get('https://financialmodelingprep.com/api/v3/quote/AAPL?apikey=demo'); // Grab the first (and only) stock object from the response array return response.data[0]; }; const quoteValue = await fetchStockQuote(); console.log(quoteValue); message.reply(`Here's your quote: Symbol: ${quoteValue.symbol} Name: ${quoteValue.name} Price: $${quoteValue.price}`); }
One quick extra check: Make sure you've properly imported axios at the top of your file (either const axios = require('axios'); for CommonJS or import axios from 'axios'; for ES modules) — without that, you'll get an axios is not defined error.
内容的提问来源于stack exchange,提问作者dilonv2
相关产品推荐
相关产品推荐

