0

I'm getting an error with my typescript code in Ionic/Angular. The error output is:

[22:55:09] typescript: C:/xampp/htdocs/project x/anonymous-social/src/pages/chat/chat.ts, line: 103 Expected 1 arguments, but got 0.

L102: if(!this.isUserThreadEmpty) {

L103: let threadKey = this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient).push().key;

L104: let recipientData = {

I don't know exactly why it's happening, I would guess that push() needs an argument but all the stackoverflow answers I saw online regarding how to get a key in firebase pointed towards this solution so I'm not sure what's going on.

The actual code is:

if(!this.isUserThreadEmpty) {
  let threadKey = this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient).push().key;
  let recipientData = {
    recipient: this.recipient,
    threadId: threadKey,
    displayName: this.displayName,
  }

Any ideas? I need to pass the key to the recipientData... What am I doing wrong?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Simon
  • 2,498
  • 3
  • 34
  • 77
  • 1
    `I would guess that push() needs an argument` - yes, it does ... `all the stackoverflow answers I saw online` ... can you link to one that uses `.push()` on an array like that? – Jaromanda X Jun 08 '18 at 03:04
  • @JaromandaX this one for instance: https://stackoverflow.com/questions/38768576/in-firebase-when-using-push-how-do-i-get-the-unique-id-and-store-in-my-databas/38768630#38768630 – Simon Jun 08 '18 at 03:07
  • @JaromandaX the official docs as well: https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push – Simon Jun 08 '18 at 03:10
  • oh, so `this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient)` isn't an array - sorry, my bad – Jaromanda X Jun 08 '18 at 03:26

1 Answers1

2

You're using AngularFire2. The push method in AngularFire2 requires an argument.

The question and documentation you linked are for the regular Firebase JavaScript SDK, where the argument to push is optional.

Your best option is to do this with the regular JavaScript SDK, as there is no advantage in doing it through the AngularFire2 wrapper:

let threadKey = firebase.database().ref().push().key;

From Jane's comment: to use the Firebase JavaScript SDK you will need to:

import * as firebase from 'firebase/app';

Update (2018-06-08): I just found out that there's also a dedicated method AngularFireDatabase.createPushId() that creates a push ID. So I think in your case that'd be database.createPushId().

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Interesting, ok. How do I load that `firebase` variable? I get `Cannot find name 'firebase'.` I'm currently only loading in `AngularFireDatabase` in my `.ts` file... – Simon Jun 08 '18 at 03:51
  • 1
    For future reference on AngularFire2 will need to import: `import * as firebase from 'firebase/app';` and then you can simply use `firebase.database().ref().push().key;` to generate a key. – Simon Jun 08 '18 at 12:33