Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

javascript - 为什么刷新后页面无法正确加载?(Why is my page not loading correctly when refreshed?)

I am currently trying to use Mithril to create some sort of social media clone.

(我目前正在尝试使用秘银创建某种社交媒体克隆。)

When a user logs in, he is redirected to his timeline and a request is sent to fetch its feed.

(当用户登录时,他将被重定向到他的时间轴,并发送一个请求以获取其提要。)

This part works just fine but when I try reloading the page, an error occurs.

(这部分工作正常,但是当我尝试重新加载页面时,发生错误。)

Here's my mithril code for the timeline :

(这是我的时间表的秘银代码:)

var m = require("mithril");

var user = sessionStorage.getItem("user");

var data;
m.request({
    method: "GET",
    url: "https://tinyinsta-257216.appspot.com/_ah/api/tinyInstaAPI/v1/timeline?pseudo="+user,
    }).then(function(result) {
        data = result;
});

module.exports = {


    view: function() {

        return m("main", [
            m("button.button.post[type=button]", {
                onclick: function(e) {m.route.set("/post");}
            }, "Poster"),
            m("h1", "Timeline"),
            m("h2", user),
            m("table", 
                m("tr",
                        m("th", "Auteur"),
                        m("th", "Image"),
                        m("th", "Message"),
                        m("th", "Likes")
                ),
                data.items.map(row => 
                    m("tr.post", 
                        m("td.postPseudo", row.properties.pseudo),
                        m("td.postImage", 
                                m("img", {src: row.properties.image.value})
                        ),
                        m("td.postMessage", row.properties.message),
                        m("td.postLikes", row.properties.likes),
                        m("button.button[type=button]",{
                            onclick: function(e) {}
                        }, "Like")
                    )   
                )
            )
        ])


    }
}

And here's the error when reloading the page, occuring line 31 (at the part data.items.map...) :

(这是重新加载页面时发生的错误,出现在第31行(在data.items.map ...部分):)

TypeError: "data is undefined"

From what I understand, the code outside the module.exports section is not executed again when reloading, but I have poor understanding in how Javascript works.

(据我了解,重新加载时不会再次执行module.exports部分之外的代码,但是我对Javascript的工作方式了解甚少。)

I tried putting this section in the oninit() function, to no effect.

(我试图将此部分放在oninit()函数中,但没有任何效果。)

Thank you for your help.

(谢谢您的帮助。)

  ask by joss-enet translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...