gpt4 book ai didi

javascript - TypeError : $scope. messages.push 不是一个函数

转载 作者:行者123 更新时间:2023-11-28 04:59:07 25 4
gpt4 key购买 nike

我正在我的 Ionic 应用程序中与 Firebase 进行聊天集成,我尝试显示以下消息:

  1. 已发送
  2. 来自之前的时间

到目前为止,我可以很好地检索以前显示的消息,或者提交新消息也没有问题。

但是我无法找到同时执行两个功能的方法。

如果我使用以下代码,则会出现此错误:

TypeError: $scope.messages.push is not a function

有什么想法吗?

这是我作为我的 Controller 的代码:

.controller('Messages',['$scope', '$ionicScrollDelegate', '$timeout', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope,  $ionicScrollDelegate, $timeout, $firebaseArray, CONFIG, $document, $state) {
$scope.hideTime = false;
var myId = firebase.auth().currentUser.uid;
var otherId = "0LhBcBkECAXn6v12lA2rlPIsQNs2";
var discussion = myId + otherId;

var isIOS = ionic.Platform.isWebView() && ionic.Platform.isIOS();

//Recieve messages
var ref = firebase.database().ref('/messages/' + discussion);
ref.orderByChild("timestamp").on("value", function(snapshot1) {
console.log(snapshot1.val() + "HEY");
$scope.messages = snapshot1.val();

})

//Send messages
$timeout(function(){
$scope.sendMessage = function() {

var d = new Date();
var g = new Date();
d = d.toLocaleTimeString().replace(/:\d+ /, ' ');
g = g.toLocaleTimeString().replace(/:\d+ /, ' ') + new Date();



$scope.messages.push({
userId: myId,
text: $scope.data.message,
time: d
});

$scope.sendMessages = firebase.database().ref('/messages/' + discussion).push({
userId: myId,
text: $scope.data.message,
time: d,
timestamp: g
});

console.log($scope.messages);

delete $scope.data.message;
$ionicScrollDelegate.scrollBottom(true);

};

$scope.inputUp = function() {
if (isIOS) $scope.data.keyboardHeight = 216;
$timeout(function() {
$ionicScrollDelegate.scrollBottom(true);
}, 300);

};

$scope.inputDown = function() {
if (isIOS) $scope.data.keyboardHeight = 0;
$ionicScrollDelegate.resize();
};

$scope.closeKeyboard = function() {
// cordova.plugins.Keyboard.close();
};
$scope.data = {};
$scope.myId = myId;
$scope.messages = [];



})

}]);

这是我的 HTML 代码:

<ion-view view-title="Chat Detail">
<ion-content class="padding" start-y="430">
<!-- *messages / content messages -->
<ol class="messages">
<!-- *self / container self messages -->
<li ng-repeat="message in messages" ng-class="{other: message.userId != myId}" class="self">
<!-- *avatar / avatar image -->
<div class="avatar">
<img src="img/82.jpg">
</div>
<!-- *msg / messages -->
<div class="msg">
<p>{{ message.text }}</p>
<time>
<i class="icon ion-ios-clock-outline"></i>
{{message.time}}
</time>
</div>
</li>
</ol>
</ion-content>
<!-- *bar-detail / color bar and input -->
<ion-footer-bar keyboard-attach class="item-input-inset bar-detail">
<label class="item-input-wrapper">
<input type="text" placeholder="Type your message" on-return="sendMessage(); closeKeyboard()" ng-model="data.message" on-focus="inputUp()" on-blur="inputDown()" />
</label>
<a class="button button-icon icon ion-arrow-return-left" ng-click="sendMessage()" ></a>
</ion-footer-bar>

感谢您提前提供的建议和评论!

最佳答案

19小时后找到解决方案,太荒谬了:

我必须删除那部分代码。

$scope.messages.push({
userId: myId,
text: $scope.data.message,
time: d
});

并确保检索消息的值为“on”而不是“once”,以便与 Firebase 的实时数据库配合使用。

关于javascript - TypeError : $scope. messages.push 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310314/

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