node.js - 将数据从我的其余 api 提取到我的 VueJS 组件的数组数据属性中

标签 node.js rest express vue.js

我已经研究了 Vue.js 几个星期,并使用 NodeJS Express 开发了一个 Rest API 后端,我想连接到它。我已经让 Rest API 正常工作并为我提供 JSON。现在,我尝试获取该 json 并将其存储在我的 Vue 组件之一中的数组中,使用 vue-resource

{
    "status": 200,
    "error": null,
    "response": [
        {
            "TxnID": "277731-1507125498",
            "SalesRep": null,
            "QuoteNumber": "20822PF",
            "QuoteModified": "2017-10-23T17:46:25.000Z",
            "Customer": "Dquard",
            "Country": "Indonesia",
            "QuoteAmount": 9106,
            "SalesOrderNumber": "19289",
            "SalesOrderAmount": 9106,
            "IsFullyInvoiced": "true",
            "status": "WIN 100%",
            "type": null,
            "margin": null
        },
        {
            //more data
        }
    ]
}

我想将每个响应放入一个数组对象中。我正在尝试用一种方法来做到这一点:

methods: {
    loadData () {
      this.$http.get(this.url)
        .then(response => {
          return response.json()
        }).then(data => {
          const resultArray = []
          for (let key in data.response) {
            resultArray.push(data[key])

          }
          console.log(resultArray)
          // this.salesOrders = resultArray
        })
    }

但是当控制台显示 resultArray 时,它只是一堆 未定义:

enter image description here

我需要更改 Rest API 提供 json 的方式吗?理想情况下,我希望 json 中的每个响应对象将每个记录作为键/值对提供。例如 SalesRep: 'Jason' 而不是像 'SalesRep':'Jason'

这样的字符串/字符串

最佳答案

问题出在您的 for 循环中,您没有访问响应数组。

for (let key in data.response) {
    resultArray.push(data.response[key])
}

关于node.js - 将数据从我的其余 api 提取到我的 VueJS 组件的数组数据属性中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52521732/

相关文章:

javascript - writeFile 不等待变量被实例化

Node.js,PostgreSQL 错误 : no pg_hba. 主机的 conf 条目

javascript - 通过 Express 提供时,Index.html 不呈现组件

javascript - 如何测试nodejs中流的错误?

node.js - 保存时Docker Nodemon未启动

node.js - 在镜像大小较小的 Docker Alpine 镜像中创建非 root 用户?

c# - 使用 WebGet 的可选 UriTemplate 参数

c# - Asp.Net Web API 中小数的自定义模型绑定(bind)器

java - 使用 java 在 eclipse 中创建的 Jersey Tomcat Web 服务器的 404 错误

node.js - 访问 Multer 缓冲区对象