HTTP Get请求不适用于FireStore

时间:2020-05-13 01:28:11

标签: typescript google-cloud-firestore get google-cloud-functions http-get

我正在尝试向Google Firebase发出GET请求,我正在尝试尝试使用Firebase-functions-helper,当我尝试发出GET请求时,我收到了尝试中的消息,但是我没有要显示的数据。
要发出我正在使用邮递员的GET请求,在邮递员上,当执行GET请求时,我得到以下信息:

Get Success

这是功能代码:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as firebaseHelper from 'firebase-functions-helper/dist';
import * as express from 'express';
import * as bodyParser from 'body-parser';

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

const app = express();
const main = express();

main.use(bodyParser.json());
main.use(bodyParser.urlencoded({extended: false}));
main.use('/api/v1', app);

const sensorCollection = 'dadosusuarios';
export const webApi = functions.https.onRequest(main);

app.patch('/sensor/:sensorId', async(req, res) => {
try{
    await firebaseHelper.firestore.updateDocument(db, sensorCollection, 
 req.params.sensorId, req.body);
    res.status(200).send("Update Success");
}catch (error){
    res.status(204).send("Patch Error");
}
});

//this is the get part
app.get('/getsensor/', async(req, res) => {
try{
    await firebaseHelper.firestore.getDocument(db, sensorCollection, 
'mykey')
    .then(data => console.log(JSON.stringify(data)));
    res.status(200).send("Get Success");
}catch(error){
    res.status(204).send("Get Error");
}
});

当我获取请求时,我需要数据为JSON格式,以便可以从Arduino访问
我的Firestore由以下组成:

Collection name: dadosusuarios
Document: mydocId
Collection Fields:
          led1: false
          led2: True
          led3: false
          name: 'undefined'
          quantidade: 90
          sensores: 'sensor'

0 个答案:

没有答案