$firebaseArray $indexFor() 在给定值时找不到键
$firebaseArray $indexFor() not finding key when given value
我有这个 Firebase:
users: {
userId: {
notifications: {
notificationId: "Notification"
}
}
}
当给定 "Notification" 时,我试图找到它的 notificationId(它是从 push() 方法生成的)以便我最终可以删除它。根据文档,$indexFor() 方法应该为我做这件事。这是我的代码:
var ref = new Firebase('https://url.firebaseio.com/');
$scope.dismissNotification = function(notification) {
var notificationRef = ref.child('users/' + $scope.currentUser.id + '/notifications');
var notifications = $firebaseArray(notificationRef);
notifications.$loaded().then(function(data) {
console.log(data);
console.log(data.$indexFor(notification));
}).catch(function(error) {
console.log('Error: ' + error);
});
};
第一个日志是正确的对象,里面有我正在寻找的通知字符串,但是第二个日志 returns -1,当我想要它 return 相关的 notificationId有了它。
不确定您要完成什么,但这是查找给定值的键的最简单方法:
var notificationRef = ref.child('users/' + $scope.currentUser.id + '/notifications');
var query = notificationRef.orderByValue().equalTo(notification);
query.once('child_added', function(snapshot) {
console.log(snapshot.key());
});
我有这个 Firebase:
users: {
userId: {
notifications: {
notificationId: "Notification"
}
}
}
当给定 "Notification" 时,我试图找到它的 notificationId(它是从 push() 方法生成的)以便我最终可以删除它。根据文档,$indexFor() 方法应该为我做这件事。这是我的代码:
var ref = new Firebase('https://url.firebaseio.com/');
$scope.dismissNotification = function(notification) {
var notificationRef = ref.child('users/' + $scope.currentUser.id + '/notifications');
var notifications = $firebaseArray(notificationRef);
notifications.$loaded().then(function(data) {
console.log(data);
console.log(data.$indexFor(notification));
}).catch(function(error) {
console.log('Error: ' + error);
});
};
第一个日志是正确的对象,里面有我正在寻找的通知字符串,但是第二个日志 returns -1,当我想要它 return 相关的 notificationId有了它。
不确定您要完成什么,但这是查找给定值的键的最简单方法:
var notificationRef = ref.child('users/' + $scope.currentUser.id + '/notifications');
var query = notificationRef.orderByValue().equalTo(notification);
query.once('child_added', function(snapshot) {
console.log(snapshot.key());
});