$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - generate custom key with $add angularfire - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin12PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far