【问题标题】:Is there any way to return my json object有什么办法可以返回我的 json 对象
【发布时间】:2020-01-18 13:32:50
【问题描述】:

我最近正在编写一个 Javascript Web 应用程序,用户可以在其中浏览公共 Instagram 个人资料并下载图像。

因此,我使用 JSON 对象来存储用户个人资料中的所有信息。

我的函数如下所示:

receiveProfile(username) {
    var url = "https://www.instagram.com/" + username + "/?__a=1";

    var resultObject;

    fetch(url).then(function (response) {
        return response.json();
    }).then(function (data) {

        resultObject = new UserProfile(
            data.graphql.user.full_name,
            data.graphql.user.biography,
            data.graphql.user.profile_pic_url,
            data.graphql.user.external_url,
            data.graphql.user.edge_owner_to_timeline_media.edges,
            data.graphql.user.edge_followed_by,
            data.graphql.user.edge_follow
        );

        return resultObject;
    }).catch(function () {
        console.log("Booo");
    });

    return resultObject;
}

我有一个名为“JsonService”的对象的实例,它获取此方法的返回值,即某个用户的 UserProfile。然后 UserProfile 将存储为我的 JsonService 的字段。但是当我设置我的领域 this.userProfile = receiveProfile(username); 并尝试控制台记录它,它总是在我的浏览器中显示“未定义”。

如何正确地将对象传递给我的 JsonService 的字段。

【问题讨论】:

  • 欢迎来到 Stack Overflow。你的函数在 fetch 完成之前返回,所以它总是返回 undefined
  • 这种情况的一种可能的解决方案是使用Promise,在此处进一步阅读:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 感谢您的回答。我阅读了文档并了解了使用 Promise 的优点,但现在我对实现此方法仍有一些疑问。我现在应该将我的 fetch-structure 放在 Promise 中吗?
  • 哦,现在我明白了。只需在 fetch 前面添加一个“return”,以便返回整个 fetch 并实例化一个包含我的 UserProfile 对象的新 Promise。感谢您的所有建议!

标签: javascript jquery json ajax instagram


【解决方案1】:

fetch() 方法返回一个 Promise 这解决了 Response 到那个请求,不管它是否成功。

receiveProfile 函数在 fetch() 块完成之前返回 resultObject(最初未定义)。它应该等待 promise 解决。

你有两个选择:

1。使用异步/等待

async receiveProfile(username) {
    const url = `https://www.instagram.com/${username}/?__a=1`;

    const response = await fetch(url);

    if (response.status !== 200) {
        throw new Error(response.status);
    }

    const data = await response.json();

    return new UserProfile(
        data.graphql.user.full_name,
        data.graphql.user.biography,
        data.graphql.user.profile_pic_url,
        data.graphql.user.external_url,
        data.graphql.user.edge_owner_to_timeline_media.edges,
        data.graphql.user.edge_followed_by,
        data.graphql.user.edge_follow
    );
}

演示:

// Dummy class
class UserProfile {
    constructor(full_name, biography, profile_pic_url, external_url, edges, edge_followed_by, edge_follow) {
        this.full_name = full_name;
        this.biography = biography;
        this.profile_pic_url = profile_pic_url;
        this.external_url = external_url;
        this.edges = edges;
        this.edge_followed_by = edge_followed_by;
        this.edge_follow = edge_follow;
    }
}

async function receiveProfile(username) {
    const url = `https://www.instagram.com/${username}/?__a=1`;

    const response = await fetch(url);

    if (response.status !== 200) {
        throw new Error(response.status);
    }

    const data = await response.json();

    return new UserProfile(
        data.graphql.user.full_name,
        data.graphql.user.biography,
        data.graphql.user.profile_pic_url,
        data.graphql.user.external_url,
        data.graphql.user.edge_owner_to_timeline_media.edges,
        data.graphql.user.edge_followed_by,
        data.graphql.user.edge_follow
    );
}

receiveProfile('instagram').then(function(user) {
    console.log(user);
});

2。没有异步/等待

receiveProfile(username) {
    const url = `https://www.instagram.com/${username}/?__a=1`;

    return fetch(url).then(function(response) {
        return response.json();
    }).then(function(data) {
        return new UserProfile(
            data.graphql.user.full_name,
            data.graphql.user.biography,
            data.graphql.user.profile_pic_url,
            data.graphql.user.external_url,
            data.graphql.user.edge_owner_to_timeline_media.edges,
            data.graphql.user.edge_followed_by,
            data.graphql.user.edge_follow
        );
    }).catch(function(error) {
        console.error('Error: ', error);
    });
}

演示:

// Dummy class
class UserProfile {
    constructor(full_name, biography, profile_pic_url, external_url, edges, edge_followed_by, edge_follow) {
        this.full_name = full_name;
        this.biography = biography;
        this.profile_pic_url = profile_pic_url;
        this.external_url = external_url;
        this.edges = edges;
        this.edge_followed_by = edge_followed_by;
        this.edge_follow = edge_follow;
    }
}

function receiveProfile(username) {
    const url = `https://www.instagram.com/${username}/?__a=1`;

    return fetch(url).then(function (response) {
        return response.json();
    }).then(function (data) {
        return new UserProfile(
            data.graphql.user.full_name,
            data.graphql.user.biography,
            data.graphql.user.profile_pic_url,
            data.graphql.user.external_url,
            data.graphql.user.edge_owner_to_timeline_media.edges,
            data.graphql.user.edge_followed_by,
            data.graphql.user.edge_follow
        );
    }).catch(function(error) {
        console.error('Error: ', error);
    });
}

receiveProfile('instagram').then(function (user) {
    console.log(user);
});

【讨论】:

  • 感谢您的精彩回答。唯一缺少的部分是:如何将输出存储在变量中?
  • 您可以在then() 回调中执行此操作,因为fetch() 返回一个Promise。 let userProfile; receiveProfile('instagram').then(function (user) { userProfile = user; });
猜你喜欢
  • 2013-08-08
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2016-03-18
  • 2011-05-22
  • 2023-01-24
相关资源
最近更新 更多