gpt4 book ai didi

javascript - 发送消息时获取 ID - Firebase 和 AngularFire

转载 作者:行者123 更新时间:2023-11-29 15:19:16 25 4
gpt4 key购买 nike

我有一个消息传递“应用程序”,我需要在创建消息时获取 ID。以下是所有代码:

<html ng-app="sampleApp">

<body ng-controller="SampleCtrl">
<ul>
<li ng-repeat="message in messages">
<input ng-model="message.message" ng-change="messages.$save(message)" />
<input ng-model="message.by" ng-change="messages.$save(message)" />
<button ng-click="messages.$remove(message)">Delete Message</button>
</li>
</ul>
<form ng-submit="addMessage()">
<input ng-model="newMessageText" />
<input ng-model="newMessageBy" />
<button type="submit">Add Message</button>
</form>

<!-- Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<!-- Firebase -->
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>

<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/2.3.0/angularfire.min.js"></script>
<script>
var config = {
apiKey: "xxx",
authDomain: "xxx",
databaseURL: "xxx",
projectId: "xxx",
storageBucket: "xxx",
messagingSenderId: "xxx"
};
firebase.initializeApp(config);
var app = angular.module("sampleApp", ["firebase"]);
app.controller("SampleCtrl", function ($scope, $firebaseArray) {
var ref = firebase.database().ref().child("messages");
$scope.messages = $firebaseArray(ref);
$scope.addMessage = function () {

$scope.messages.$add({
message: $scope.newMessageText,
by: $scope.newMessageBy
});
};
});
</script>
</body>

</html>

发送消息时,它会创建一个 ID 及其下的消息内容。因此,我需要获取发送的消息的 ID,并将 ID 写入 Firebase。

这是数据库的样子:

{
"messages" : {
"-KrsHPUi7pIWTQ1qgO4q" : { // I NEED TO GET THIS
"message": "blah blah blah...",
"by": "john"
}
},
"ids" : {
"first": "-KrsHPUi7pIWTQ1qgO4q" // AND PUT IT HERE
}

最佳答案

如何获取id

$scope.messages.$add({
message: $scope.newMessageText,
by: $scope.newMessageBy
}).then(function(ref) {
var id = ref.key;
// do with the key what ever you want
})

关于javascript - 发送消息时获取 ID - Firebase 和 AngularFire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45767277/

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