javascript - generate custom key with $add angularfire - Stack Overflow

How can I generate a custom key in array inside angularfire when I add a new record with the $add funct

How can I generate a custom key in array inside angularfire when I add a new record with the $add function. Just look at when the is the ment below in the source code. This is my code below but still get the random key generated by firebase.

register : function(user){
  return simpleLogin.$createUser({
    email: user.email,
    password: user.password
  }).then(function(regUser){
    //console.dir(regUser);
    var ref = new Firebase(FIREBASE_URL + 'users');
    var firebaseUsers = $firebaseArray(ref);
    var userInfo = {
      date: Firebase.ServerValue.TIMESTAMP,
      regUser: regUser.uid,
      firstname: user.firstname,
      lastname: user.lastname,
      email: user.email
    }

    //this is when i want to generate the key
    firebaseUsers.$add(userInfo).then(function(ref) {

    });
  });
},//register

How can I generate a custom key in array inside angularfire when I add a new record with the $add function. Just look at when the is the ment below in the source code. This is my code below but still get the random key generated by firebase.

register : function(user){
  return simpleLogin.$createUser({
    email: user.email,
    password: user.password
  }).then(function(regUser){
    //console.dir(regUser);
    var ref = new Firebase(FIREBASE_URL + 'users');
    var firebaseUsers = $firebaseArray(ref);
    var userInfo = {
      date: Firebase.ServerValue.TIMESTAMP,
      regUser: regUser.uid,
      firstname: user.firstname,
      lastname: user.lastname,
      email: user.email
    }

    //this is when i want to generate the key
    firebaseUsers.$add(userInfo).then(function(ref) {

    });
  });
},//register

Share Improve this question edited May 31, 2015 at 15:18 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked May 30, 2015 at 18:10 Nejmeddine JammeliNejmeddine Jammeli 1,0379 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Calling $add will simply always result in a so-called push id. If you don't want to use those push ids to identify the objects, the solution is to not call $add/push.

Instead you can just access the child that you want to create directly:

    var ref = new Firebase(FIREBASE_URL + 'users');
    var userInfo = {
      date: Firebase.ServerValue.TIMESTAMP,
      regUser: regUser.uid,
      firstname: user.firstname,
      lastname: user.lastname,
      email: user.email
    }

    ref.child(regUser.uid).set(userInfo);

Two things to note here:

  1. the snippet creates the child node under regUser.uid. If you need another key, you can substitute that.
  2. this snippet doesn't use AngularFire, but instead uses the Firebase JavaScript SDK directly. Since AngularFire is built on the JavaScript SDK, they interoperate perfectly. So if you need an operation that isn't immediately obvious in AngularFire, it might be worth it to check whether you can acplish it (more easily) with the regular Firebase JavaScript SDK.

with the help of mester Frank van Puffelen above I modify the code :

register : function(user){
  return simpleLogin.$createUser({
    email: user.email,
    password: user.password
  }).then(function(regUser){
    //console.dir(regUser);
    var ref = new Firebase(FIREBASE_URL + 'users');
    var firebaseUsers = $firebaseArray(ref);
    var userInfo = {
      date: Firebase.ServerValue.TIMESTAMP,
      regUser: regUser.uid,
      firstname: user.firstname,
      lastname: user.lastname,
      email: user.email
    }

    //the added portion of code
    ref.child(regUser.uid).set(userInfo);

    firebaseUsers.$add(userInfo).then(function(ref) {

    });





  });
},//register

and i got the result i want , in angularfire API there is no functions ( 'child()' and set() ) but as mentionned in the post of mester Frank van Puffelen angularfire API is built up on firebase API .

and this is my capture image from my database firebase.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745674171a4639583.html

相关推荐

  • javascript - generate custom key with $add angularfire - Stack Overflow

    How can I generate a custom key in array inside angularfire when I add a new record with the $add funct

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信