繁体   English   中英

如何从axios promise对象获取数据?

[英]How can I get data from axios promise object?

我想从axios Promise数据中获取变量,但它没有分配我的变量。

const axios = require('axios');

let url = 'localhost:3000'
let id = ''
let token = 'my-token'

let api = axios.create({
    baseURL: url,
    withCredentials: false,
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        "Access-Control-Allow-Origin": "*"
    }
})

const getNotices = function (id, token) {
    var arr = []
    let result = api
        .request({
            url: `/notices/${id}/${token}`,
            method: "get",
        })
        .then(res => {
            arr = res.data
            console.log('in', arr)
        })
    console.log('out', arr)
    return arr
}

getNotices(id, token)

我想打印“ arr”,但结果是api的“内部”和“外部”不同。

下面代码的结果是

out []
in [Array(3)]

我不知道为什么不一样。

我想将此代码用于vuejs模块,但无法导出数据。


编辑-tyskr的代码是这样的

const getNotices = function (id, token) {
    return api
        .request({
            url: `/notices/${id}/${token}`,
            method: "get",
        })
        .then(res => {
            return res.data
        })
}

getNotices(id, token).then(function (arr) {
    console.log(arr)
})

但是我还不能像这样访问

const getNotices = function (id, token) {
    return api
        .request({
            url: `/notices/${id}/${token}`,
            method: "get",
        })
        .then(res => {
            return res.data
        })
}
var result = []
getNotices(id, token).then(function (arr) {
    result = arr
})

我认为他们有不同的范围,对吗?

您需要使用异步等待,如下所示。

const getNotices = async function (id, token) {
var arr = []
let result = await api
    .request({
        url: `/notices/${id}/${token}`,
        method: "get",
    })
    .then(res => {
        arr = res.data
        console.log('in', arr)
    })
    console.log('out', arr) // this line is executing before the response you get from url so we need to use async await
    return arr

如果您以以下方式重新排列代码:

const getNotices = function (id, token) {
    var arr = []
    return api
        .request({
            url: `/notices/${id}/${token}`,
            method: "get",
        })
        .then(res => {
            arr = res.data
        })
}

getNotices(id, token).then(function(arr){
    console.log(arr); //should solve your issue
})

然后,您应该能够获得一致的arr值。

让我知道那是否行不通...

您必须获得响应的第一个索引

 const axios = require('axios'); let url = 'localhost:3000' let id = '' let token = 'my-token' let api = axios.create({ baseURL: url, withCredentials: false, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', "Access-Control-Allow-Origin": "*" } }) const getNotices = function(id, token) { var arr = [] let result = api .request({ url: `/notices/${id}/${token}`, method: "get", }) .then(res => { arr = res.data[0] console.log('in', arr) }) console.log('out', arr) return arr } getNotices(id, token) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM