如何使用Firebase函数将从网站爬网的值插入Firebase实时数据库?

时间:2019-04-09 19:56:08

标签: android firebase firebase-realtime-database

我正在为各种在线可用产品的实时价格更新开发一个应用程序。但是我无法将数据发送到实时数据库。我正在获取如何在实时数据库中进行引用。

`

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

`

1 个答案:

答案 0 :(得分:0)

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