1 回答
TA贡献1770条经验 获得超3个赞
请在下面的代码中找到mark1和mark2。
mark1:您不需要导出此方法,只需在渲染 ejs 视图之前使用它即可。当你在这个函数中返回你的json时,你不需要在这里添加额外的await。
mark2:我将get_data函数放在这里是因为我看到你在router.get("/youtube/callback"). 因此,在向用户呈现 ejs 视图之前,您需要调用get_data函数来获取所需的数据。然后,只需用数据渲染它即可。
router.get("/youtube/callback", function(req, res) {
// Create an OAuth2 client object from the credentials in our config file
const oauth2Client = new OAuth2(
CONFIG.oauth2Credentials.client_id,
CONFIG.oauth2Credentials.client_secret,
CONFIG.oauth2Credentials.redirect_uris[0]
);
if (req.query.error) {
// The user did not give us permission.
return res.redirect("/");
} else {
oauth2Client.getToken(req.query.code, function(err, token) {
if (err) return res.redirect("/");
// Store the credentials given by google into a jsonwebtoken in a cookie called 'jwt'
res.cookie("jwt", jwt.sign(token, CONFIG.JWTsecret));
// return res.redirect("/get_some_data");
if (!req.cookies.jwt) {
// We haven't logged in
return res.redirect("/");
}
// Add this specific user's credentials to our OAuth2 client
oauth2Client.credentials = jwt.verify(req.cookies.jwt, CONFIG.JWTsecret);
// Get the youtube service
const service = google.youtube("v3");
const url = `https://www.googleapis.com/oauth2/v1/userinfo?access_token=${token[Object.keys(token)[0]]}`;
// ================ mark 1 ====================
const get_data = async () => {
try {
const response = await nf(url);
const json = await response.json();
return json;
} catch (error) {
console.log(error);
}
};
// Get 50 of the user's subscriptions (the channels they're subscribed to)
service.subscriptions
.list({
auth: oauth2Client,
mine: true,
part: "snippet,contentDetails",
maxResults: 50
})
// ================ mark 2 ====================
// remember to add async here
.then(async (response) => {
// ================ mark 2 ====================
const diomerda = await get_data()
// Render the profile view, passing the subscriptions to it
return res.render("./user/dashboard", { subscriptions: response.data.items, diomerda: diomerda });
});
});
}
});
添加回答
举报