Angularfire: Can't push list item with $key

Created on 4 Dec 2016  路  10Comments  路  Source: angular/angularfire

If I try to create an object with predefined key, firebase returns an error. Is it expected?

let list = af.database.list('/items');
list.push({
    $key: new Date().getTime(),
    name: 'Test'
});
zone.js:390Error: Uncaught (in promise): Error: Firebase.push failed: first argument  contains an invalid key ($key) in property 'items'.  Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"
Error: Firebase.push failed: first argument  contains an invalid key ($key) in property 'items'.  Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"

I'm now using the update function instead, but I have no way of evaluating if an item existed before.

Most helpful comment

Hi,
with this syntax it's ok 馃檪:

let newKey = new Date().getTime();
let list = af.database.object(`/items/${newKey}`).set({ name: 'Test' });

All 10 comments

Basic project setup with the error described above.
ng-firebase-test.zip

None of these characters are valid in key names in Firebase: . # $ / [ ]

@jsayol tried timestamp and simple dummy strings like foo. Are you talking about the object property $key?

$key seems to be the correct attribute to read from the database, but how to set it in a push?
screen shot 2016-12-05 at 13 03 21

If you want to set a key just like in the examples you showed then simply remove the $ symbol, like this:

let list = af.database.list('/items');
list.push({
    key: new Date().getTime(),
    name: 'Test'
});

With Angularfire2, the $key property of an object holds the value of the parent key of that object. AF2 does that on the fly for you, you don't need to set that property (nor you can). So based on your screenshot, if you retrieve the object at items/-KYDqbl2s0EZ0YoN9R_F:

{
  key: 1480939173058,
  name: "Test"
}

then the key property will be 1480939173058 and the $key property will be -KYDqbl2s0EZ0YoN9R_F.

So following Firebase's posts tutorial, how could you accomplish a DB like theirs? Where the paths are setup as seen here.

The key in the user posts collection is the actual user ID, not some randomly generated one.

The guide lists two methods for updating lists. Push is used to generate new records, with auto-generated ids, and update is used to set a specific key. See here.

All issues have to follow the issue template provided. I understand this is asking a lot and is generally annoying when you feel your question is straightforward, but just trust that it's critical to keep things streamlined and manageable for our team. I'm closing this for now because it doesn't adhere to these requirements. Feel free to resubmit according to the template, being absolutely sure to include a working, minimal repro of the problem and version info.

Note that how-to questions are better suited for Stack Overflow or a discussion group.

Hi,
with this syntax it's ok 馃檪:

let newKey = new Date().getTime();
let list = af.database.object(`/items/${newKey}`).set({ name: 'Test' });

Thank you very much

Was this page helpful?
0 / 5 - 0 ratings