gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot call method 'push' of undefined

转载 作者:行者123 更新时间:2023-11-30 08:52:51 27 4
gpt4 key购买 nike

我尝试让 firebase 绑定(bind)与 knockoutjs 一起工作,但无法让它们工作,所以我正在尝试使用 angularjs 进行一些测试。所以我下载了 angularjs,我只是想输入/输出一些数据。所以现在我的 app.js 中有这个:

/* Controllers */

angular.module('MyCtrl3', ['$scope', 'angularFire'])
.controller ("MyCtrl3", function ($scope, angularFire) {
$scope.addMsg = function ($scope, angularFire) {
var url = 'https://kingpinapp.firebaseio.com/msgs';
var promise = angularFire(url, $scope, 'msgs', []);
$scope.msgs.push({name: "Firebase", desc: "is awesome!"});

}
});

然后我的 html 就是:

<!doctype html>
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="css/app.css"/>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>

<div>Angular seed app: v<span app-version></span></div>

<div ng-controller="MyCtrl3">

<button ng-click="addMsg()">Add</button>
</div>

<!-- bunch of scripts -->
</body>
</html>

只是试图让添加按钮推送到 firebase。我在控制台中收到此错误:Uncaught TypeError: Cannot call method 'push' of undefined。添加了一个 jsfiddle:http://jsfiddle.net/NHaZz/5/

最佳答案

AngularFire 返回一个 promise ,因此在 promise 实现之前您不能操作数组。您可以使用 then 函数执行此操作,如下所示:

angular.module('MyCtrl3', ['$scope', 'angularFire'])
.controller ("MyCtrl3", function ($scope, angularFire) {
var url = 'https://kingpinapp.firebaseio.com/msgs';
var promise = angularFire(url, $scope, 'msgs', []);
promise.then(function() {
$scope.addMsg = function ($scope, angularFire) {
$scope.msgs.push({name: "Firebase", desc: "is awesome!"});
}
});
});

关于javascript - 未捕获的类型错误 : Cannot call method 'push' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com