【问题标题】:Fetch promise not defined even when console.log shows the value即使 console.log 显示该值,也未定义 Fetch Promise
【发布时间】:2019-12-12 12:51:14
【问题描述】:

我目前正在尝试从 API (AlphaVantage) 获取一些数据并将数据作为参数传递给 axios.post 到我的 MongoDB,但我得到了错误消息,即使我可以得到该变量也没有定义console.log 的值。

static insertPost(shareName) {
  axios.get(url)
  .then(res => {name = res.data['Meta Data']['2.Symbol'];
  console.log(res.data['Time Series (15min)']['2019-08-02 15:45:00']['1. 
  open']);
  price = res.data['Time Series (15min)']['2019-08-02 15:45:00']['1. open'];
})
.then( res => {
  return axios.post(url, {
    name,
    price
})});}

收到此错误:

Uncaught (in promise) ReferenceError: price is not defined 在评估时

即使名称和 console.log 工作正常,因为它们在第一个承诺中,我不知道发生了什么,有什么建议吗?

【问题讨论】:

    标签: vue.js fetch


    【解决方案1】:

    nameprice 属于不同的范围。要将其传递给.then 中的下一个链,您需要返回值

    static insertPost(shareName) {
      axios.get(url)
        .then(res => {
          const name = res.data['Meta Data']['2.Symbol']
          console.log(res.data['Time Series (15min)']['2019-08-02 15:45:00']['1.open']);
          const price = res.data['Time Series (15min)']['2019-08-02 15:45:00']['1. open'];
          return { name, price }
        })
        .then(res => {
          return axios.post(url, {
            name: res.name,
            price: res.price
          })
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-12
      • 2012-12-02
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      相关资源
      最近更新 更多