您好,由于某种原因,它显示为错误:
错误:[ngRepeat:dupes]不允许在转发器中重复。使用“跟踪依据”表达式指定唯一键。中继器:mess in messages,重复键:string:f,重复值:f
但是我不明白为什么,我个人没有看到任何重复的东西...
Here是我的Cloud9文件,用于查看代码并进行更改...
这是我的Chat.html:
<h1 style = "text-align:center; position:relative; left:-80px"> Universal Chat </h1>
<div id = "rectangle" >
<ul id="example-messages" class="example-chat-messages">
<li ng-repeat = "mes in messagess">
{{mes}}
</li>
</ul>
</div>
<textarea class="form-control" id="chatbox" rows="1" ng-model = "textValue">
</textarea>
<a class="btn right" id = "submitButton" ng-click = "submit()">
<span class="left title">Submit Comment</span>
<span class="right icon icon-comment"><span class="slant-right"></span></span>
</a>
和我的chatController.js:
app.controller('chatController', ["$scope", 'AuthService', "Auth", "Ref", "$rootScope", "$firebaseArray", "$firebaseObject",
function($scope, AuthService, Auth, Ref, $rootScope, $firebaseArray, $firebaseObject) {
var postsArray = {}
console.log(AuthService.User.email) //Gives me correct Email
console.log(AuthService.User.password) //Gives me correct password
console.log(AuthService.User.uid) //Gives me correct uid
console.log(AuthService.User.username) //Gives me correct username
some = $firebaseArray(Ref.child("Posts").child(AuthService.User.username));
console.log((some))
var postsFirebase = $firebaseObject(Ref)
var username = AuthService.User.username
$scope.messagess = {}
$scope.submit = function() {
if (!($scope.textValue)) {
}
else {
some.$add({
Posts: $scope.textValue
}).then(function(authData) {
console.log(authData.path)
Ref.child("Posts").child(username).child(authData.path.o[2]).on("value", function(snapshot) {
console.log(snapshot.val().Posts)
$scope.messagess.push(snapshot.val().Posts)
snapshot.val().Posts
}, function(errorObject) {
console.log("The read failed: " + errorObject.code);
});
}).catch(function(errorObject){
alert("See the console for more details")
console.error(errorObject.code)
})
}
}
}
])
SnapShot.val就像您在我的firebase中提交的当前帖子:
这不是重复项。这些其他地方的方法没有用。
最佳答案
重复错误表示其f
已重复,因此我认为消息是字符串,而不是数组。您想要做的是显示每条消息。但是现在,您正在遍历消息本身,即:
<li ng-repeat="mes in 'message'">
第一项使用
m
,第二项使用e
,第三项使用s
。而且,如您所料,当它注意到下一个(重复的)s
时,它将引发错误。您应该遍历消息数组,即:
<li ng-repeat="mes in ['message']">
这将正确显示它们。请检查在此行中保存到范围的内容:
$scope.messagess = snapshot.val().Posts
它应该是一个数组。
但是,请注意,修复它后,仍应使用
track by
,因为两次发布相同的消息将再次触发该错误。<li ng-repeat="mes in messages track by $index">
相关:Angular ng-repeat Error "Duplicates in a repeater are not allowed."]
如果仍然有问题,请尝试创建Minimal, Complete, and Verifiable example,以便我们提供帮助。
更新:
根据您提供的数据结构(
Posts
是我假设的字符串)屏幕,您应该将范围分配更改为:$scope.messagess = snapshot.val()
然后在视图中读取所有值,即
<li ng-repeat="mes in messages">{{ mes.Posts }}</li>
但是,我发现这种结构(和命名)很奇怪。