第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

表達如何將值從 .js 承諾傳遞到 .ejs 視圖

表達如何將值從 .js 承諾傳遞到 .ejs 視圖

catspeake 2023-09-28 09:51:04
我試圖將值從路由 js 文件(auth.js)傳遞到 ejs 視圖(dashboard.ejs)這是 auth.js 中的代碼:const express = require("express");const google = require('googleapis').google;const jwt = require('jsonwebtoken');const CONFIG = require("../config/passport-google");const nf = require('node-fetch');const router = express.Router();// Google's OAuth2 clientconst OAuth2 = google.auth.OAuth2;router.get("/youtube", 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]  );  // Obtain the google login link to which we'll send our users to give us access  const loginLink = oauth2Client.generateAuthUrl({    access_type: "offline", // Indicates that we need to be able to access data continously without the user constantly giving us consent    scope: CONFIG.oauth2Credentials.scopes // Using the access scopes from our config file  });  return res.render("./home/g-login", { loginLink: loginLink });});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));diomerda 變量是一個承諾,我已經(jīng)通過 service.subscriptions 下面的函數(shù)渲染視圖(它正確地將訂閱值發(fā)送到儀表板視圖),那么我如何也將我的 diomerda 變量傳遞到該視圖?我正在嘗試,module.exports正如您在代碼中看到的那樣,但似乎我無法require在 ejs 文件內使用
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經(jīng)驗 獲得超3個贊

請在下面的代碼中找到mark1和mark2。


mark1:您不需要導出此方法,只需在渲染 ejs 視圖之前使用它即可。當你在這個函數(shù)中返回你的json時,你不需要在這里添加額外的await。


mark2:我將get_data函數(shù)放在這里是因為我看到你在router.get("/youtube/callback"). 因此,在向用戶呈現(xiàn) ejs 視圖之前,您需要調用get_data函數(shù)來獲取所需的數(shù)據(jù)。然后,只需用數(shù)據(jù)渲染它即可。


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 });

          });

  

      });

    }

  });


查看完整回答
反對 回復 2023-09-28
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號