【发布时间】:2020-07-09 14:50:54
【问题描述】:
我开始使用 express 开发这个 node.js 应用程序,通过 http 请求将传感器数据发送到我的数据库(具体来说是从 arduino)。该应用程序的主要目的是从 url 中获取传感器的值,并在我的 cloud firestore 中创建一个新文档。
var admin = require('firebase-admin');
const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const express = require('express');
var serviceAccount = require("./minicapcollar-firebase-adminsdk-ovdpm-cda3767493.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://minicapcollar.firebaseio.com"
});
let db = admin.firestore();
const app = express();
app.use(cors);
//Declare the field view for the timestamp
let FieldValue = require('firebase-admin').firestore.FieldValue;
app.get('/send/sensorData', (req, res) => {
var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
var lat = req.query.lat/1000000;
var lon = req.query.lon/1000000;
var hr = req.query.hr;
var et = req.query.et;
var it = req.query.it;
//Rest of the values
//Declare the index of the collar id
let indexRef = db.collection('dogs').doc(id);
//Declare the index of the position
let posRef = indexRef.collection('position').doc();
//Declare the index of the heartrate
let hrRef = indexRef.collection('heartrate').doc();
//Declare the index of the external temperature
let etRef = indexRef.collection('external_temperature').doc();
//Declare the index of the internal temperature
let itRef = indexRef.collection('temperature').doc();
//Save the current time
var time = FieldValue.serverTimestamp();
//Set position
let setPos = posRef.set({
timestamp: time,
value: new admin.firestore.GeoPoint(lat,lon)
}).then(function() {
console.log("Position data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
//Set heartrate
let setHr = hrRef.set({
timestamp: time,
value: hr
}).then(function() {
console.log("Heartrate data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
//Set external temperature
let setET = etRef.set({
timestamp: time,
value: et
}).then(function() {
console.log("External temperature data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
//Set internal temperature
let setIT = itRef.set({
timestamp: time,
value: it
}).then(function() {
console.log("Data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
res.send(`All sensor data sent`);
});
app.get('/send/pos', (req, res) => {
var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
var lat = req.query.lat/1000000;
var lon = req.query.lon/1000000;
//Rest of the values
//Declare the index of the collar id
let indexRef = db.collection('dogs').doc(id);
//Declare the index of the position
let posRef = indexRef.collection('position').doc();
//Save the current time
var time = FieldValue.serverTimestamp();
//Set position
let setPos = posRef.set({
timestamp: time,
value: new admin.firestore.GeoPoint(lat,lon)
}).then(function() {
console.log("Position data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
res.send(`Position sent`);
});
app.get('/send/hr', (req, res) => {
var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
var hr = req.query.hr;
//Declare the index of the collar id
let indexRef = db.collection('dogs').doc(id);
//Declare the index of the heartrate
let hrRef = indexRef.collection('heartrate').doc();
//Save the current time
var time = FieldValue.serverTimestamp();
//Set heartrate
let setHr = hrRef.set({
timestamp: time,
value: hr
}).then(function() {
console.log("Heartrate data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
res.send(setHr & `Heartrate value sent`);
});
app.get('/send/temp', (req, res) => {
var id = 'HpwWiJSGHNbOgJtYi2jM'; //variable to store the id of the collar, TODO: req.query.id
var et = req.query.et;
var it = req.query.it;
//Declare the index of the collar id
let indexRef = db.collection('dogs').doc(id);
//Declare the index of the external temperature
let etRef = indexRef.collection('external_temperature').doc();
//Declare the index of the internal temperature
let itRef = indexRef.collection('temperature').doc();
//Save the current time
var time = FieldValue.serverTimestamp();
//Set external temperature
let setET = etRef.set({
timestamp: time,
value: et
}).then(function() {
console.log("External temperature data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
//Set internal temperature
let setIT = itRef.set({
timestamp: time,
value: it
}).then(function() {
console.log("Data saved to Firestore");
return null;
}).catch(function(error) {
console.log("Got an error: ", error);
});
res.send(`Temperature values sent`);
});
exports.app = functions.https.onRequest(app);
完成应用程序后,我在本地主机上对其进行了测试,它运行良好。我能够毫无问题地将新数据写入云 Firestore。部署应用程序后,在发送请求时,我收到 403(错误:禁止您的客户端无权从该服务器获取 URL /app/send/pos/?lat=11111111&lon=22222222。)
在 Firestore 规则中,我指定任何人都可以读写以查看是否是问题所在,但问题仍然存在。
我也尝试按照本教程进行操作:https://www.youtube.com/watch?v=LOeioOKUKI8,但我在尝试访问“http://baseURL.firebaseapp.com/timestamp”时遇到了同样的问题,部署时出现 403 错误(在 localhost 上也可以正常工作)
注意:这是我第一次使用 node.js,请原谅任何不好的做法。我是一名电气工程专业的学生,所以编程不是我的主要优势。在此先感谢您的帮助!
【问题讨论】:
-
尝试删除函数并重新部署。还要确保您的 Firebase CLI 完全是最新的。
-
我已经多次这样做了。我什至部署了示例函数(作为我提供的视频链接中的那个)并且问题仍然存在。关于 firebase CLI,我确保它已更新。我也尝试过使用 google cloud shell,非常感谢您的快速响应!
-
我建议联系 Firebase 支持以寻求帮助。 support.google.com/firebase/contact/support
-
谢谢 Dough,我会联系他们,让您知道他们是否能够解决我的问题。
-
@VictorManuel 看看this answer 看看它是否有帮助。昨天有几个人在 Firebase Slack 上讨论它,他们按照这些步骤解决了它。
标签: node.js firebase express google-cloud-firestore google-cloud-functions