AngularFire $ add-TypeError:undefined不是一个函数 [英] AngularFire $add—TypeError: undefined is not a function
本文介绍了AngularFire $ add-TypeError:undefined不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
TypeError:undefined不是一个函数$ $ http:// localhost:9000 / bower_components / angular / angular $ b.b $ scope.sendComment(http:// localhost:9000 / scripts / controllers / post.js:22:38)
。 js:10847:21
...
这是我的controller @ post.js
16 $ scope.sendComment = function(){
17 var newComment = {
18 user:$ scope .currentUser,
19 text:$ scope.currentText
20
21};
22 var promise = CommentService.add(newComment);
23 promise.then(function(name){
24 console.log(name);
25});
26};
这里是我的服务(有问题的add函数在底部)
(function(angular){
'use strict';
angular.module('codesApp')。service 'CommentService',函数(FBURL,$ q,$ firebase){
var commentRef = new Firebase(FBURL).child('comments');
var fireComment = $ firebase(commentRef);
return {
childAdded:function childAdded(limitNumber,cb){
commentRef.startAt()。limit(limitNumber).on('child_added',function(snapshot){
var val = snapshot.val();
cb.call(this,{
user:val.user,
text:val.text,
name:snapshot.name()
});
});
},
add:function addComment(comment){
return fireComment。$ add(comment);
},
....
k在这一整晚。任何帮助非常感谢!感谢!
$ firebase(commentRef)只能让你获得一个可以调用$ add()的firebase数组的一半。 / p>您必须在$ firebase(commentRef)的返回值上另外调用$ asArray(),如下所示:
var fireCommentArray = $ firebase(commentRef)。$ asArray();
然后您可以
fireCommentArray。$ add(comment)
When I try to substitute $add for push, I get this error.
TypeError: undefined is not a function
at Scope.$scope.sendComment (http://localhost:9000/scripts/controllers/post.js:22:38)
at http://localhost:9000/bower_components/angular/angular.js:10847:21
...
Here's my controller @ post.js
16 $scope.sendComment = function () {
17 var newComment = {
18 user: $scope.currentUser,
19 text: $scope.currentText
20
21 };
22 var promise = CommentService.add(newComment);
23 promise.then(function(name) {
24 console.log(name);
25 });
26 };
Here's my service (the problematic add function is toward the bottom)
(function(angular) {
'use strict';
angular.module('codesApp').service('CommentService', function(FBURL, $q, $firebase) {
var commentRef = new Firebase(FBURL).child('comments');
var fireComment = $firebase(commentRef);
return {
childAdded: function childAdded(limitNumber, cb) {
commentRef.startAt().limit(limitNumber).on('child_added', function(snapshot) {
var val = snapshot.val();
cb.call(this, {
user: val.user,
text: val.text,
name: snapshot.name()
});
});
},
add: function addComment(comment) {
return fireComment.$add(comment);
},
....
I've been stuck on this all night. Any help is hugely appreciated! Thanks!
解决方案
$firebase(commentRef) only gets you half way to a firebase array that can call $add().
You must additionally call $asArray() on the return value of $firebase(commentRef) like so:
var fireCommentArray = $firebase(commentRef).$asArray();
Then you can
fireCommentArray.$add(comment)
这篇关于AngularFire $ add-TypeError:undefined不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文