为什么我的AngularFire push()不起作用?

时间:2015-02-28 11:47:36

标签: angularjs undefined firebase angularfire controllers

我正在尝试通过运行典型的待办教程来学习Angular和Firebase,但我正在修改应用程序。而不是待办事项列表,我实际上是创建'用户'。

Firebase已正确设置,从Firebase中提取对象数据也是如此,我可以通过ng-repeat看到所有内容按预期填充。

但我正在尝试使用.push()方法“添加新成员”,我收到错误。请看一下:

以下是我上传到Firebase(只有一个用户)

的JSON数据示例
{
  "members": {
    "-JWT5y43YFy1mGirVVS2": {
    "date": 1410328158691,
    "firstname": "Michael",
    "lastname": "Jordan",
    "project": "sample project description",
    "image": "http://telehealth.org/wp-content/images/user-placeholder.jpg",
    "upcoming": "PTO on Thursday",
    "status": {
      "color":  "red",
      "contact": {
      "email": "test@email.com",
      "yahoo": "yahooIM"
        },
      "projects": {
        "projectone": "project one",
        "projecttwo": "project two",
        "projectthree": "project three"
        }
      }
    }
  }
}

这是我的.html代码

<h1 id="teamTitle">Team Members</h1>
<div class="addMember">
    <p>add member</p>
    <form class="formmember"
        name="memberform"
        ng-submit="addMember()"
        novalidate>
            <div class="inputwrapper">
                <input type="text" name="firstname" placeholder"First Name" ng-required="true">
                <button type="submit" class="btn"
                ng-disabled="memberform.$invalid">Add</button>
            </div>
    </form>
</div>
<ul class="cbp-rfgrid" ng-repeat="member in members">
    <li class="{{member.status.color}}">
        <img src="{{member.image}}">
        <div>
            <h3>{{member.firstname}}</h3>
            <button>View</button>
        </div>
    </li>
</ul>

这是我的Controller.js

myApp.controller('MembersController', function($scope, $firebase){

var ref = new Firebase('https://scrumcheck.firebaseio.com/members');
var members = $firebase(ref);

$scope.members = members.$asObject();

$scope.addMember = function(){
    members.$push({
        firstname: $scope.firstname,
        date: Firebase.ServerValue.TIMESTAMP
    });
}

});

控制台日志显示以下错误:

 Error: Firebase.set failed: First argument contains undefined in property 'firstname'

我希望通过向成员推送一个只有firstname键的对象,它允许我这样做。我只允许push()一个整个对象,其中包含所有键值吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

检查您的$ scope.firstname模型,问题出在哪里。确保它不是空值并且正确定义。