如何解决Firebase-在同一套件中同时测试Firestore和实时数据库的问题
当我对Firestore云功能和实时DB云功能运行带有单元测试的测试套件(Mocha)时,出现以下错误。当我注释掉以前的Firestore云功能测试时,错误消失了。可能是什么原因造成的?
1) updateEventgoerCounter
updates the stagehalls/ID/activeViewers counter:
TypeError: Cannot read property 'databaseURL' of null
at resourceGetter (node_modules/firebase-functions/lib/providers/database.js:116:54)
at Function.get (node_modules/firebase-functions/lib/cloud-functions.js:151:17)
at hasPath (node_modules/firebase-functions-test/node_modules/lodash/lodash.js:6153:24)
at Function.has (node_modules/firebase-functions-test/node_modules/lodash/lodash.js:13185:32)
at Object.wrap (node_modules/firebase-functions-test/lib/main.js:28:19)
at Context.it (test/stagehall/updateEventgoerCounter.test.ts:47:26)
setup.test.ts:
process.env.GCLOUD_PROJECT = 'sampleApp'
const test = require('firebase-functions-test')({
databaseURL: "https://sampleApp.firebaseio.com",projectId: "sampleApp",storageBucket: "sampleApp.appspot.com",},'./serviceAccountKey.json');
export default test
实时数据库测试:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import onFail from "../../utils/errorHandler";
!admin.apps.length && admin.initializeApp();
const database = admin.database();
// Note: This is a Realtime Database trigger,*not* Cloud Firestore.
export const updateEventgoerCounter = functions.database
.ref("foo/{fooId}/eventgoers")
.onUpdate(async (change,context) => {
if (change.after.val() === change.before.val()) return false;
const snapshot = await database
.ref(`foo/${context.params.stageId}/eventgoers`)
.once("value")
.catch(onFail);
functions.logger.log("updateEvent:",snapshot);
const activeViewers = snapshot
.val()
.filter(
(fooEventgoerData: { active: boolean }) =>
fooEventgoerData.active === true
).length;
return database
.ref("foo/${fooId}/")
.set({ activeViewers })
.catch(onFail);
});
firestore测试:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
import * as _ from 'lodash';
import axios from 'axios';
import * as queryString from 'query-string';
import * as url from 'url';
!admin.apps.length && admin.initializeApp();
const db = admin.firestore();
const onFail = (message: string) => {
return (error: any) => {
functions.logger.error(message,error);
};
};
// adds duration and youtube video id to new videos saved to stagehalls
export const addMetaDataToVideo = functions.firestore
.document('foo/{id}')
.onUpdate(async (change,context) => {
const stageId = context.params.id;
const data = change.after.data().media;
const videosMissingMetadata = _.filter(data,video => !video.duration);
const apiCallPromises = videosMissingMetadata.map((video) => {
const searchParams = url.parse(video.url).search || "";
const query = queryString.parse(searchParams);
const youtubeKey = functions.config().youtube_api.key;
// query youtube api for video details
return axios(`https://www.googleapis.com/youtube/v3/videos?id=${query.v}&part=contentDetails&key=${youtubeKey}`)
.then((response: any) => {
Object.assign(data[video.id],{
youtubeId: query.v,duration: response.data.items[0].contentDetails.duration
});
})
.catch(onFail('addMetaDataToVideo - youtube api fetch failed!'));
})
await Promise.all(apiCallPromises);
return db.doc(`foo/${fooId}`)
.set({
media: data
},{ merge: true })
.catch(onFail('addMetaDataToVideo - update failed!'));
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。