javascript - 在解决 promise 之前,函数正在重新调整未定义

标签 javascript node.js angular vue.js

我有一个 vue 程序,它使用服务从 API 调用获取产品。

getProducts() {
    axios.get("https://jsonplaceholder.typicode.com/posts").then(function (response) {
        return response;
    }).catch(function(error){
        console.log(error);
    })
}

在我需要调用它的 vue 页面中,我在created() 钩子(Hook)中进行调用,如下所示:

  created() {
    //this returns a promise
    productservices.getProducts().then(response =>{
      this.products = response.data;
    }).catch(error => console.log(error))
    .finally(() =>{
      consol.log("All done!");
    })
  },

我可以毫无问题地调用该函数,但它正在运行 .then() 并返回 Undefined。为什么会发生这种情况?我该如何修复?

最佳答案

getProducts() {
    return axios.get("https://jsonplaceholder.typicode.com/posts")
}

试试这个。

关于javascript - 在解决 promise 之前,函数正在重新调整未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075059/

相关文章:

javascript - Nodejs/Mongoose 将变量传递给 mongoose 函数

node.js - 如何在 grunt.js 上绑定(bind)服务器和客户端?

ios - ionic 5 Modal 在 iOS 上显示空白页面

node.js - 如何停止知道名称的永久进程

Angular - 对所有组件导入使用桶文件

javascript - 使用 Angular 4 将页眉/页脚与内容分开

javascript - 找到JS中调用该函数的函数?

javascript - 在表单中添加值

Javascript 检查键盘状态

javascript - 如何使用 MVC 模型为 google map 坐标构建 javascript 数组