我对firebase和node.js非常陌生。我想读取一些数据并将这些数据推送到我的Firebase实时数据库中。我尝试过:
var axios = require('axios');
var data = '{"currData":1,"alarms":1}';
var firebase = require('firebase');
require('firebase/database');
const firebaseConfig = {
apiKey: "<myApiKey>",
authDomain: "<myproject>.firebaseapp.com",
databaseURL: "https://<myURL>.firebaseio.com",
projectId: "<myProjectID>",
storageBucket: "<myprojectName>.appspot.com",
messagingSenderId: "<mySenderID>",
appId: "1:<myappID>"
};
let app = firebase.initializeApp(firebaseConfig)
let db = app.database()
let sensorDataRef = db.ref('sensorData')
var config = {
method: 'post',
url: 'http://myIP/api',
headers: {
'Connection': 'keep-alive',
'Origin': 'http://myIP',
'Referer': 'http://myIP/',
'Accept-Language': 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
'Cookie': 'username=myUsername; SID=mySID; group=myGroup' },
data : data
};
axios(config)
.then(function (response) {
console.log(response.data);
return firebase.database().ref().once('value').then(function(snapshot) {
var username = (snapshot.val() && snapshot.val()) || 'Anonymous';
console.log(snapshot.val())
});
var currentDatas = response.data["currData"];
for(var attributename in currentDatas){
// console.log(attributename+": "+ JSON.stringify(currentDatas[attributename]));
firebase.database().ref('users/' + attributename).set(currentDatas[attributename]);
}
})
.catch(function (error) {
console.log(error);
});
当我运行节点this.js时,它不会将任何内容推送到数据库。但是我可以记录我的API数据。