簡體   English   中英

如何使用 firebase 函數將從網站抓取的值插入到 firebase 實時數據庫中?

[英]How to insert value crawlled from a website to the firebase realtime database using firebase functions?

我正在為各種在線可用產品的實時價格更新制作一個應用程序。 但是我無法將數據發送到實時數據庫。 我是為了獲取如何在實時數據庫中引用。

const functions = require('firebase-functions');
var admin = require("firebase-admin");
const request = require('request');
const cheerio = require('cheerio');
// URL
const URL = "https://www.flipkart.com/search?q=mobiles";
admin.initializeApp({
    "serviceAccount": "./InfoApp-2b1936e80782.json",
    "databaseURL": "https://infoapp-234ff.firebaseio.com"
  });
  var ref = admin.database().ref("hello"); //('data').child(data).child('specs');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
  exports.helloWorld = functions.https.onRequest((request, response) => {
     let $ = cheerio.load(body); 
     $('div._1HmYoV > div.col-10-12>div.bhgxx2>div._3O0U0u').each(function(index){ 
         const data = $(this).find('div._1UoZlX>a').attr('href'); 
         const name = $(this).find('div._1-2Iqu>div.col-7-12>div._3wU53n').text();
         console.log(data);
         var a = ref.push();
               a.set({
                author: "gracehop",
                title: "Announcing COBOL, a New Programming Language"
              });
     });
     response.send("activity !");
  });

`

push 函數不返回引用。 你可以替換這個:

var a = ref.push();
a.set({
    author: "gracehop",
    title: "Announcing COBOL, a New Programming Language"
});

和:

ref.push({
    author: "gracehop",
    title: "Announcing COBOL, a New Programming Language"
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM