传输项目,并从用户的使用AngularJS和火力地堡 [英] Transfer Items to and from users Using AngularJS and Firebase

查看:102
本文介绍了传输项目,并从用户的使用AngularJS和火力地堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我与AngularJS和火力地堡工作,并试图建立两个用户之间的简单交换。现在我的数据结构下的用户/ UID /,然后他们的电子邮件,他们加入日期和黄金成立。

在金(用户/ UID /金)我有派捕捉的数量,时间和对象(电子邮件)。这低于code片段。它还更新其总的黄金。

现在我被困在更新他们送金的人。我捕捉到的电子邮件地址,但在范围都涉及到登录的用户的电流。我怎么会更新新用户的用户/ UID /金的金额,时间和电子邮件谁是从,以更新他们的总金顺/接收?

我觉得我可能要对这个错误的方式,任何帮助将是AP preciated,谢谢!

  ledger.controller('TransferController',函数(
$范围,$火力点,$ routeParams,$位置$ rootScope,FIREBASE_URL){    $ scope.whichuser = $ routeParams.uId;
    $ scope.goldsends = goldsendList;    VAR REF =新的火力地堡(FIREBASE_URL +'/用户/+ $ scope.whichuser +'/金/'+'/发送/');
    VAR hopperRef =新的火力地堡(FIREBASE_URL +'/用户/+ $ scope.whichuser +'/金/');
    VAR usersRef = ref.child(用户);
    VAR goldsendList = $火力点(REF)$ asArray()。
    $ scope.sendGold =功能(){
        VAR sendgoldObj = $火力点(REF); //这个变种具有对sendgoldObj。$推变种符合以下了,这就是它        VAR MYDATA的= {
            金额:$ scope.user.amount,
            电子邮件:$ scope.user.email,
            日期:Firebase.ServerValue.TIMESTAMP
        };        sendgoldObj。$推(MYDATA的)。然后(函数(){
            // $ location.path('/ myledger /'); //页面重定向
        }); //发送到火力地堡数据。        如果($ scope.currentUser.gold.total - Math.abs($ scope.user.amount)大于0){//            VAR hopperRefff = hopperRef.child(金);
            hopperRef.update({
                总:$ scope.currentUser.gold.total - $ scope.user.amount
            }); //更新黄金总            VAR收到=新的火力地堡(FIREBASE_URL);
            ref.child(用户)。orderByChild(电子邮件)。equalTo(EMAILADDRESS)。一旦('值',函数(SNAP){
                的console.log(?snap.name()+(snap.val()===空'不':'不')+'存在');
            }); //试图找到用户发送黄金        } //如果用户有足够的黄金声明
        其他{
            返回{
                范围: {
                    errormessage的:你不\\'有足够的钱,
                }
            };
            的console.log(没有足够的钱!);
        } //其他笔记足够的黄金声明    } // sendgold
}); // TransferController


解决方案

一个值设置为用户只是如果用户检查是否存在变异。一旦你有一个快照,您可以通过调用回到一个引用 REF

  ref.child(用户)。orderByChild(电子邮件)。equalTo(EMAILADDRESS)。一旦('值',函数(SNAP){
    。snap.ref()更新({总:snap.val()总金额+});
});

不,这仅仅是一个样品,所以你可能需要更新它实际的数据结构。

更新

以上将让你的用户节点的值。

您要么需要捕捉一次('child_added的forEach 在('值'。我给两个例子。

聆听 child_added

  ref.child(用户)。orderByChild(电子邮件)。equalTo(EMAILADDRESS)。一旦('child_added',函数(SNAP){
    。snap.ref()更新({总:snap.val()总金额+});});

循环在值的一个示例

  ref.child(用户)。orderByChild(电子邮件)。equalTo(EMAILADDRESS)。一旦('值',函数(SNAP){
    snap.forEach(功能(childsnap){
        。childsnap.ref()更新({总:snap.val()总金额+});});

下面是与两个样本jsbin: http://jsbin.com/fenavu/1 /编辑?JS,控制台。请注意,code在这里写出了 ref.toString(),它给你的节点的完整URL(因为每一块在火力地堡数据都有自己的唯一的URL)。这可以是要弄清楚什么网址找到节点映射到一个方便的途径。

Ok, so I'm working with AngularJS and Firebase and trying to create a simple exchange between two users. Right now my data structure is set up under "users/uId/" and then their email, date they joined, and gold.

Under gold (users/uId/gold) I have "sent" which captures the amount, time and to whom (email). This is the code snippet below. It also updates their total gold.

Now I'm stuck updating the person they're sending the gold to. I capture the email address, but everything under scope relates to the current logged in user. How would I update the new users users/uId/gold/received with the amount, time and email who it was from, along with updating their total gold?

I feel like I might be going about this the wrong way, any help would be appreciated, thanks!

ledger.controller('TransferController', function (
$scope, $firebase, $routeParams, $location, $rootScope, FIREBASE_URL) {

    $scope.whichuser = $routeParams.uId;
    $scope.goldsends = goldsendList;

    var ref = new Firebase(FIREBASE_URL + '/users/' + $scope.whichuser + '/gold/' + '/sent/');
    var hopperRef = new Firebase(FIREBASE_URL + '/users/' + $scope.whichuser + '/gold/');
    var usersRef = ref.child("users");
    var goldsendList = $firebase(ref).$asArray();


    $scope.sendGold = function () {
        var sendgoldObj = $firebase(ref); //this var has to match the sendgoldObj.$push var down below, and that's it

        var myData = {
            amount: $scope.user.amount,
            email: $scope.user.email,
            date: Firebase.ServerValue.TIMESTAMP
        };

        sendgoldObj.$push(myData).then(function () {
            //   $location.path('/myledger/');  //page redirect
        }); //data sent to firebase.

        if ($scope.currentUser.gold.total - Math.abs($scope.user.amount) > 0) { //

            var hopperRefff = hopperRef.child("gold");
            hopperRef.update({
                "total": $scope.currentUser.gold.total - $scope.user.amount
            }); //update total gold

            var receive = new Firebase(FIREBASE_URL);
            ref.child('users').orderByChild('email').equalTo(emailAddress).once('value', function (snap) {
                console.log(snap.name() + (snap.val() === null ? ' DOES NOT' : ' does') + ' exist');
            }); //trying to find user to send gold to

        } //if user has enough gold statement
        else {
            return {
                scope: {
                    errormessage: 'You don\'t have enough money',
                }
            };
            console.log("not enough money!");
        } //else note enough gold statement

    } //sendgold
}); //TransferController

解决方案

Setting a value to the user is just a variation of checking if that user exists. Once you have a snapshot, you can get back to a ref by calling ref

ref.child('users').orderByChild('email').equalTo(emailAddress).once('value', function (snap) {        
    snap.ref().update({ "total": snap.val().total + amount });
});

Not that this is just a sample, so you'll probably have to update it for your actual data structure.

Update

The above will get you the value of the users node.

You either need to capture the once('child_added' or forEach over the on('value'. I'll give an example of both.

Listening to child_added:

ref.child('users').orderByChild('email').equalTo(emailAddress).once('child_added', function (snap) {        
    snap.ref().update({ "total": snap.val().total + amount });

});

An example of looping over the value:

ref.child('users').orderByChild('email').equalTo(emailAddress).once('value', function (snap) {        
    snap.forEach(function(childsnap) {
        childsnap.ref().update({ "total": snap.val().total + amount });

});

Here's a jsbin with both samples: http://jsbin.com/fenavu/1/edit?js,console. Note that the code here writes out the ref.toString(), which gives you the full URL of the node (since every piece of data in Firebase has its own unique URL). That can be a handy way to figure out what URL your node maps to.

这篇关于传输项目,并从用户的使用AngularJS和火力地堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆