14

I am working on Angular2 project using Firebase.

I need to push all query results under the same key in this.guestPush.

I have a multiple select element which contains different user levels. 4, 6, 9.

When one level is selected, the resulting object is saved in the guestPush array, but when another level is selected the following error occurs.

I get this error:

ERROR TypeError: this.guestPush[("filter" + i)].push is not a function

Here is my code :

  guestPush: any[] = new Array;

  level.forEach((lvl, index) => {

    this.db.object(`levelsUsers/${lvl}/users`)
      .subscribe(users => {

        if (this.guestPush['filter_' + i]) {
          this.guestPush['filter_' + i].push(users);
        } else {
          this.guestPush['filter_' + i] = users;
        }

      }, err => console.log(err));

  });

Edit ***

The users object which contains the user or users which match the filter:

Object {-KmSjAaxdCUvrPEQ8InI: Object, $key: "users", $exists: function}
  -KmSjAaxdCUvrPEQ8InI: Object
    admin:"false"
    description:"desc"
    ...
    ...
    verified:false
  __proto__:Object
  exists:function ()
  $key:"users"
  __proto__:Object

And this.guestPush[i] = users; creates an object like this:

[Object]
 0:Object
 -KmSjAaxdCUvrPEQ8InI: Object
    admin:"false"
    description:desc"
    ...
    ...
    verified:false
    __proto__:Object
  $exists:function ()
  $key:"users"
  __proto__:Object
 length:1
 __proto__:Array(0)

So in the next loop I want to add any new user objects next to -KmSjAaxdCUvrPEQ8InI or anything as long as it is added as a value of the 0 key.

7
  • 1
    You have the line "this.db.object", maybe changing that to a list subscription will help? If you're using angularfire, that's returning an object, so thus no push method. Reference Commented Jul 14, 2017 at 1:20
  • Since everything here's an arrow function, i'm kind of lost with this, is guestPush a variable or a property? Commented Jul 14, 2017 at 1:40
  • It's a property. Commented Jul 14, 2017 at 1:49
  • Typescipt supports future features from JS. One of those is Map. You can rewrite your code not treating your array as a map. It will be more readable and transparent for you Commented Jul 18, 2017 at 5:30
  • Where is 'i' coming from? Do you mean 'index'? Commented Jul 18, 2017 at 8:37

5 Answers 5

11
+25

Looks like you are getting an array response and you are assigning it to the object :

this.guestPush['filter_' + i] = users;

so this.guestPush['filter_'+ i] is of type array now. If you want to add another users object to it , you are essentially adding another array to existing array. In that case shouldn't you be using concat ?

if (this.guestPush['filter_' + i]) {
    this.guestPush['filter_' + i].concat(users);
} else {
    this.guestPush['filter_' + i] = users;
}

if users is not an array, then it should be

if (this.guestPush['filter_' + i]) {
    this.guestPush['filter_' + i].push(users);
} else {
    this.guestPush['filter_' + i] = [users];
}
Sign up to request clarification or add additional context in comments.

Comments

9

I ended up using

 this.guestPush.push({ [i]: { [key]: obj } });

Comments

2

@Ciprian : As per your answer.

You need a small change from your code to use. I hope this below code enough to you.

let key ='filter_';
this.guestPush.push({ key + i : users });

1 Comment

I need to add the key as object key when saving to database. each represent a user. and I also need to delete objects based on i
2

Here's your problem,

guestPush: any[] = new Array;
'filter_' + i // this will yield a string.
this.guestPush['filter_' + i])

I could define an object:

let name = {};
name['x'] = 'blah';
or name.x = 'blahblah'; // name.x does same thing as name['x']

I think your thinking name['x'] = 'blah'; is an array, when it's object syntax.

Consequently you need to think about implementing a Dictionary.

Comments

1

Typescript push to specific key in array

You need to use the splice method on the array : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

4 Comments

He's trying to access keys that aren't indexed though
I tried splice ... this.guestPush['filter_' + i].splice(1, 0, users); but it's giving me the same error. VM3282:27 ERROR TypeError: _this.guestPush[("filter_" + i)].splice is not a function at SafeSubscriber._next
@CiprianCirstea your this.guestPush is an array, why are you trying to use index of string type, did that really exist? also users is an object, you are trying to use it as array.
So what index type should I use. I tried using a number type but I was having difficulties with it. I ll try again.

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.