无法通过Firebase CLI部署Firebase规则

时间:2017-06-28 13:17:55

标签: javascript android json firebase firebase-realtime-database

我在app / functions / index.js中有一个Firebase规则文件 我试图通过此命令firebase部署--only数据库部署数据库规则,但是当我运行此命令时,它会给出此错误 错误:加载firebase.json时出错:

解析错误:functions / index.js不是受支持的配置文件类型

这是我运行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);
}
);

2 个答案:

答案 0 :(得分:1)

有关如何格式化规则的示例,请参阅Firebase CLI文档的Deployment部分以及Firebase Web Codelab部分。 firebase.json文件指定:

{
  "database": {
    "rules": "database-rules.json"
  },
  ...
}

在同一目录中,有一个database-rules.json文件,其中包含上传的规则配置。

我不是百分百确定,但很可能规则配置文件必须采用JSON格式,具体取决于您收到的错误消息:Parse Error: functions/index.js is not of a supported config file type

答案 1 :(得分:0)

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

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

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

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

    要做的第一件事是从rules删除错误的firebase.json定义:

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

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

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