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