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?

4
  • 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? Commented Jun 8, 2018 at 3:04
  • @JaromandaX this one for instance: stackoverflow.com/questions/38768576/… Commented Jun 8, 2018 at 3:07
  • @JaromandaX the official docs as well: firebase.google.com/docs/database/admin/… Commented Jun 8, 2018 at 3:10
  • oh, so this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient) isn't an array - sorry, my bad Commented Jun 8, 2018 at 3:26

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

2 Comments

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...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.