gpt4 book ai didi

javascript - 无法通过 Firebase CLI 部署 Firebase 规则

转载 作者:行者123 更新时间:2023-11-30 00:29:02 25 4
gpt4 key购买 nike

我在 app/functions/index.js 中有一个 Firebase 规则文件我正在尝试通过此命令部署数据库规则: firebase deploy --only database 但是当我运行此命令时出现此错误:

Error: There was an error loading firebase.json:

Parse Error: functions/index.js is not of a supported config file type

这是我运行 firebase init 命令时创建的 firebase.json 文件

{
"database": {
"rules": "functions/index.js"
}
}

这是我的 index.js 文件

应用程序/index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var count = 1;
exports.createNewUser = functions.auth.user().onCreate(event => {

const user = event.data; // The Firebase user.

const email = user.email || ''; // The email of the user.
const displayName = user.displayName || '';
const uid = user.uid || 'usersomething';
const photo = user.photoUrl || '';

console.log("the user" + uid);
// The display name of the user.
const userRef = admin.database().ref('/users');

userRef.child(`${uid}`).update({
'email':email,
'photo':photo
});

var date = new Date();
var start_time = date.getTime();



});

exports.createEpisode = functions.database.ref('/users/{userId}/counsellor')
.onWrite(event => {
var uid = event.params.userId;
const isACounsellor = event.data.val();
if(isACounsellor == false){
var episodeRef = admin.database().ref('/episodes');

// create an episode for the user
var newData = episodeRef.push();
var date = new Date();
var start_time = date.getTime();
var episodeId = newData.key;
newData.set(
{
'user':uid,
'goal':'',
'problem':'Anxiety',
'triggers':'',
'counsellor_uid':'',
'payment_id':'',
'pay':'',
'conversation_id':'',
'start_time':start_time,
'rating':'',
'review':'',
'paid':false,
'live_sessions':'1'
}
);
var conversation = admin.database().ref('/conversation');

// create a conversation

var x = conversation.push();
x.set(
{
'start_time':start_time,
'end_time':''
}
);
var y = x.child('messages').push().set({
'message':"hey there!",
'sender':'',
'receive':uid,
'timewrite':start_time
});

conversation.limitToLast(1).once('child_added',function(childSnapshot){
var episodeReference = admin.database().ref('/episodes');
var conversation_identifier = conversation.key | 'xyxy';

episodeReference.child(episodeId).child('conversation_id').set(childSnapshot.key);
});
var userRef = admin.database().ref('/users');
userRef.child(uid).child('episodes').child('0').set(episodeId);



}
});



exports.setTimeAndCounsellor = functions.database.ref('/episodes/{epId}/paid')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original_value = event.data.val();
if(original_value == true){
var conversation = admin.database().ref('/conversation');
var date = new Date();
var start_time = date.getTime();
var episodeReference = admin.database().ref('/episodes');
var push_id = event.params.epId;
episodeReference.child(push_id).once('value', function(snapshot){
var type = snapshot.child('pay').val() | '';
var conversation_id = snapshot.child('conversation_id').val();
var d = new Date();
if(type == 1){
d.setMinutes(d.getMinutes() + 1);
}
if(type == 2){
d.setMinutes(d.getMinutes() + 2);
}
if(type == 3){
d.setMinutes(d.getMinutes() + 3);
}

conversation.child(conversation_id).child('end_time').set(d.getTime());

});
episodeReference.on('child_added',function(childSnapshot){
var userEpisodeKey = childSnapshot.key;
var anEpisode = childSnapshot.val();
var userOfEpisode = anEpisode['user'];

if(count % 2 != 0){
var counsellor_id = 'sqHdZojaTUTe26YtHTjQl32PPsq1';
episodes.child(userEpisodeKey).child('counsellor_uid').set(counsellor_id);
}else {
var counsellor_id = 'KyruKVVpNsVZl5jByhCYXyLtcwK2';
episodes.child(userEpisodeKey).child('counsellor_uid').set(counsellor_id);
}

});
}
});




exports.addToConversation = functions.database.ref('episodes/{epId}/counsellor_uid')
.onWrite(event => {
const convo = admin.database().ref('/conversation');
const episodes = admin.database().ref('/episodes');
const cid = event.data.val();
const epId = event.params.epId;
episodes.child(epId).child('conversation_id').once("value",function(dataSnapshot){
const convoId = dataSnapshot.val();
if(convoId !== ''){
convo.child(convoId).child('messages').limitToFirst(1).once("child_added",function(dataSnapshot){
const messageId = dataSnapshot.key;
convo.child(convoId).child('messages').child(messageId).child('sender').set(cid);

});

}
});
}
);

exports.notifyCounsellor = functions.database.ref('/episodes/{episodeId}/counsellor_uid')
.onWrite(event =>{
const value = event.data.val();
var user_col = admin.database().ref('/users');
user_col.child(value).child('episodes').child('0').set(episodeId);
}
);

最佳答案

您似乎混淆了可以使用 CLI 发送到 Firebase 服务器的两种类型的文件:

  1. Firebase 数据库安全规则定义了数据库服务器本身如何确保所有读/写操作均有效且经过授权。它通过一个 JSON 文件进行配置,通常称为 rules.jsondatabase-rules.jsonformat of these security rules is documented here .

  2. Cloud Functions for Firebase 是在 Google 服务器上运行的 JavaScript 函数,以响应您的 Firebase 项目中发生的事件。代码通常位于名为 index.js 的文件中。 Functions are documented here .

您要告诉 Firebase CLI 部署数据库安全规则(上面的选项 1),但要向它传递一个定义 Cloud Functions 的文件(上面的选项 2)。由于 functions/index.js 不是有效的 JSON 文件,因此该工具无法部署它。

首先要做的是从 firebase.json 中删除错误的 rules 定义:

{
"database": {
"rules": "database-rules.js"
}
}

如果您尝试仅部署项目的 Cloud Functions,请使用 firebase deploy --only functions

如果您尝试部署数据库安全规则,请确保您拥有有效的 database-rules.json 并运行 firebase deploy --only database

关于javascript - 无法通过 Firebase CLI 部署 Firebase 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44803648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com