最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Firebase pushing array - Javascript - Stack Overflow

programmeradmin11浏览0评论

I am using Firebase to store information for a workout application.

I user adds a workout name and then I push it to the database. I can continue pushing these but my issue is that it does not seem to be pushing as an array just an object. See the screen shots below...

As you can see in the console log picture the workouts property is an object not an array like I expect.

The code I'm using to push it:

    let newWorkout = {
      title: 'title',
      exercises: [{
        name: 'pulldownsnsn',
        sets: 4
      }]}

    let ref = firebase.database().ref("/userProfile/"+this.userId);

    ref.child("workouts").push(newWorkout);

I am using Firebase to store information for a workout application.

I user adds a workout name and then I push it to the database. I can continue pushing these but my issue is that it does not seem to be pushing as an array just an object. See the screen shots below...

As you can see in the console log picture the workouts property is an object not an array like I expect.

The code I'm using to push it:

    let newWorkout = {
      title: 'title',
      exercises: [{
        name: 'pulldownsnsn',
        sets: 4
      }]}

    let ref = firebase.database().ref("/userProfile/"+this.userId);

    ref.child("workouts").push(newWorkout);
Share Improve this question asked May 30, 2017 at 13:43 okayilltryokayilltry 4921 gold badge5 silver badges18 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 5

The Firebase Database stores lists of data in a different format, to cater for the multi-user and offline aspects of modern web. The -K... are called push IDs and are the expected behavior when you call push() on a database reference.

See this blog post on how Firebase handles arrays, this blog post on the format of those keys, and the Firebase documentation on adding data to lists.

Arrays are handy, but they are a distributed database nightmare for one simple reason: index element identification is not reliable when elements get pushed or deleted. Firebase database instead uses keys for element identification:

// javascript object
['hello', 'world']

// database object
{ -PKQdFz22Yu: 'hello', -VxzzHd1Umr: 'world' }

It gets tricky when using push(), because it does not actually behaves like a normal push, but rather as a key generation followed by object modification.

Example usage

firebase.database().ref('/uri/to/list').push(
    newElement,
    err => console.log(err ? 'error while pushing' : 'successful push')
)

Heres an example from firebase documentation:

const admin = require('firebase-admin');
// ...
const washingtonRef = db.collection('cities').doc('DC');

// Atomically add a new region to the "regions" array field.
const unionRes = await washingtonRef.update({
  regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
});
// Atomically remove a region from the "regions" array field.
const removeRes = await washingtonRef.update({
  regions: admin.firestore.FieldValue.arrayRemove('east_coast')
});

More info on this firebase documentation.

发布评论

评论列表(0)

  1. 暂无评论