skip to Main Content

I’m very new to Firebase. I’m building an e-commerce app using Firebase as the backend and Android Studio as the front end. In a DB, I have this tree:

Man
clothing
shorts
Product 1
Image ..
Name ..
Price ..
Size ..

Is it possible to have a size with multiple choices (for example Size 1, Size 2, Size 3, etc.)? How do I do?

2

Answers


  1. As I see in your question, those fields are of type string. Such a field can only store a single value. If you want to store multiple values in a field, then you have to choose to use an array. So you can add all those sizes in an array. So your schema might look like this:

    db
    |
    --- products
         |
         --- productId
              |
              --- name: "T-shirt"
              |
              --- price: 19.99
              |
              --- image: "https://..."
              |
              --- size
                   |
                   --- 0: "S"
                   |
                   --- 1: "M"
                   |
                   --- 2: "L"
                   |
                   --- 3: "S"
                   |
                   --- 4: "XL"
                   |
                   --- 5: "XXL"
    

    In this example, the size field is of type array and holds S at index 0, M at index 1, and so on.

    Login or Signup to reply.
  2. Data in Firebase Realtime Database is stored as JSON and it is possible to use nested JSON as for your database structure. See sample database structure below:

    {
      "products": {
        "productId": {
          "image": "path/to/image.png",
          "name": "Product Name",
          "price": 1,
          "sizes": {
            "pushId1": 1,
            "pushId2": 2
          }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search