我在尝试向 Firebase 中的行提交更新时收到错误 TypeError: Cannot set property 'songname' of null
。控制台错误表示它发生在编辑函数中的record.songname
上。我可以添加,但不能编辑行。
myApp.controller('ProductsCtrl', ['$scope', '$firebaseArray', function($scope, $firebaseArray, $http){
var myProducts = new Firebase('https://url-to-db.firebaseio.com/songs');
$scope.products = $firebaseArray(myProducts);
$scope.showForm = function(){
$scope.addFormShow = true;
$scope.editFormShow = false;
clearForm();
window.scrollTo(0, 0);
}
$scope.hideForm = function(){
$scope.addFormShow = false;
}
function clearForm(){
$scope.songname = '';
$scope.artist = '';
$scopt.tags = '';
}
$scope.addFormSubmit = function(){
$scope.products.$add({
songname:$scope.songname,
artist:$scope.artist,
tags:$scope.tags,
date:Date.now()
});
$('.messages').addClass('alert alert-success').slideDown().show().html('The song has been added').fadeOut(2000);
clearForm();
}
$scope.dateFormat = 'MM-dd-yy @ HH:mm:ss';
$scope.showProduct = function(product){
$scope.editFormShow = true;
$scope.addFormShow = false;
$scope.songname = product.songname;
$scope.artist = product.artist;
$scope.tags = product.tags;
$scope.date = product.date;
}
$scope.editFormSubmit = function(){
var id = $scope.id;
var record = $scope.products.$getRecord(id);
record.songname = $scope.songname;
record.artist = $scope.artist;
record.tags = $scope.tags;
$scope.products.$save(record);
$('.messages').addClass('alert alert-info').slideDown().show().html('The job has been edited').fadeOut(2000);
clearForm();
$('.edit-form').toggle();
}
}]);
$scope.id 属性可能未设置。尝试在 showProduct 函数或任何初始化项$scope变量的函数的范围内设置 id。
$scope.showProduct = function(product){
$scope.id = product.id;
..............