Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如果用户不发送,我可以创建日期属性吗?_Firebase_Firebase Security_Firebase Realtime Database - Fatal编程技术网

Firebase 如果用户不发送,我可以创建日期属性吗?

Firebase 如果用户不发送,我可以创建日期属性吗?,firebase,firebase-security,firebase-realtime-database,Firebase,Firebase Security,Firebase Realtime Database,我使用Firebase Bolt编译器创建了该规则 // ####### Root path / { read() = true; write() = false; } // ######## USERS PHOTOS // Allow anyone to read the list of Photos. path /users_photos { read() = true; } // All individual Photos are writable by anyone.

我使用Firebase Bolt编译器创建了该规则

// ####### Root

path / {
  read() = true;
  write() = false;
}

// ######## USERS PHOTOS
// Allow anyone to read the list of Photos.
path /users_photos {
  read() = true;
}

// All individual Photos are writable by anyone.
path /users_photos/$id is Photos {
  write() = isSignedIn();
}

type Photos {
  image: String,
  user_id: String,
  removed: Boolean,
  dt_created: InitialTimestamp,
  dt_updated: CurrentTimestamp
}


type CurrentTimestamp extends Number {
    validate() = this == now;
}

type InitialTimestamp extends Number {
  validate() = initial(this, now);
}


//
// Helper Functions
//
isSignedIn() = auth != null;
// Returns true if the value is intialized to init, or retains it's prior
// value, otherwise.
initial(value, init) = value == (prior(value) == null ? init : prior(value));
参考:

我的剧本:

/*Upload*/
VigiApp.controller('UploadController', ['$scope', 'Upload', '$timeout', 'FirebaseURL', function ($scope, Upload, $timeout, FirebaseURL) {
    // upload on file select or drop
    $scope.upload = function (file, id) {
        $('.page-spinner-bar').removeClass('ng-hide hide').addClass('ng-show show');

        id = typeof id !== 'undefined' ? id : null;
        Upload.base64DataUrl(file).then(function(base64){
            //auth
            var fbAuth = FirebaseURL.getAuth();
            //Ref
            var usersPhotosRef = FirebaseURL.child("users_photos");
            usersPhotosRef.push({'image': base64,'removed': true, 'user_id': fbAuth.uid}, function(error){
                if (error) {
                    alert('Error: Something went wrong when creating your post please try again');
                } else {
                  var newID = usersPhotosRef.key();
                  if(id !== null){
                      $('#'+id).css("background-image", "url('"+base64+"')");
                      $('#'+id).css("background-size", "100% 100%");
                  }
                }   

                $('.page-spinner-bar').removeClass('ng-show show').addClass('ng-hide hide');
            });
        });
    }     
}]);
编译

>firebase-bolt mmgv-vigiapp.bolt -o rules.json
bolt: Generating rules.json...
并部署

>firebase deploy:rules

=== Deploying to 'vivid-heat-2144'...

i  deploying rules

+  Deploy complete!

Dashboard: https://vivid-heat-2144.firebaseio.com
但我得到了一个错误:

FIREBASE WARNING: set at /users_photos/-K5VL1m04oF8s2xp8oTf failed: permission_denied 
创建的规则包括:

{
  "rules": {
    ".read": "true",
    "users_photos": {
      ".read": "true",
      "$id": {
        ".validate": "newData.hasChildren(['image', 'user_id', 'removed', 'dt_created', 'dt_updated'])",
        "image": {
          ".validate": "newData.isString()"
        },
        "user_id": {
          ".validate": "newData.isString()"
        },
        "removed": {
          ".validate": "newData.isBoolean()"
        },
        "dt_created": {
          ".validate": "newData.isNumber() && newData.val() == (data.val() == null ? now : data.val())"
        },
        "dt_updated": {
          ".validate": "newData.isNumber() && newData.val() == now"
        },
        "$other": {
          ".validate": "false"
        },
        ".write": "auth != null"
      }
    }
  }
}
当我删除日期时,它就起作用了

...
type Photos {
  image: String,
  user_id: String,
  removed: Boolean,
}
...
如何生成创建日期和更新?
请问我的错在哪里?

添加照片时,请传递以下信息:

usersPhotosRef.push({'image': base64,'removed': true, 'user_id': fbAuth.uid}
您的安全规则需要以下属性:

".validate": "newData.hasChildren(['image', 'user_id', 'removed', 'dt_created', 'dt_updated'])",
创建的
dt_
和更新的
dt_
没有神奇的“默认值”,因此您需要从应用程序代码中传入这些值:

usersPhotosRef.push({
  'image': base64,
  'removed': true, 
  'user_id': fbAuth.uid,
  'dt_created': Firebase.ServerValue.TIMESTAMP,
  'dt_updated': Firebase.ServerValue.TIMESTAMP
}

由于此代码段正在添加新记录,
dt_created
dt_updated
被设置为相同的值。更新记录时,您只需设置
dt\u updated

是否使用firebase生成的时间戳(firebase.ServerValue.timestamp)作为创建和更新日期?是的,我正在尝试=)这是可能的?我个人一直使用@AndréKool,直到最近我也这么做了。但事实证明,当您使用
Firebase.ServerValue.TIMESTAMP
时,安全规则中的
now
值保证为相同的值。