从云功能写入Firebase实时数据库

时间:2018-06-14 02:34:53

标签: javascript firebase google-cloud-functions

我正在尝试编写云功能,我可以从Youtube Api中读取并将从中获取的值发布到Firebase实时数据库。 所以,我想每隔30分钟从Youtube api上读一遍,我很清楚我应该去Firebase的CronJob。 我陷入困境的部分是,我得到了#34;错误:无法处理请求"在这个代码上,我调用代码表单url:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

// This value works! 
exports.Youtube = functions.https.onRequest((req, res) => {
  // Grab the text parameter.

  // Push the new message into the Realtime Database using the Firebase Admin SDK.
  return admin.database().ref('/YoutubeData').push(
        youtubeData()
    ).then((snapshot) => {
        // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
        return res.redirect(303, snapshot.ref.toString());
      });
});





function youtubeData()
{


    let url= "{MY API KEY}"; 


    let YoutubeApiCall = {}; 
    fetch(url) // Call the fetch function passing the url of the API as a parameter
    .then((resp) => resp.json())
    .then(function(data) {

        console.log("Data Fetched Properly"); 
        let items= data.items;
        let count = 0; 
        for ( item of items)
        {
            let title = item.snippet.title; 
            let channelTitle= item.snippet.channelTitle;
            let description = item.snippet.description;
            let thumbnailUrls = item.snippet.thumbnails.default;
            let pubDate = item.snippet.publishedAt; 
            let youtubeVideo = item.id;
            YoutubeApiCall[`${count}`] = {
                                    "title" : title,
                                    "channelTitle" : channelTitle,
                                    "thumbnail" : thumbnailUrls,
                                    "pubDate" : pubDate,
                                    "youtubeVideo" : youtubeVideo
                                    };  
            count++; 

        }
        return YoutubeApiCall; 
    })

    .catch(function() {
        console.log(" Some Error Occured "); 
    });

return YoutubeApiCall; 
}

有任何帮助吗?如果我不清楚,我道歉。

0 个答案:

没有答案