Javascript Firebase Firestore returning disordered data

Viewed 31

When I run the following using the JS client sdk

import { firestore } from "../../../firebase";

export default async (userId) => {
  const userRef = firestore.collection("users").doc(userId);
  const userDoc = await userRef.get();

 
  if (!userDoc.exists) {
     throw new Error("User doesn't exist");
  }

  console.log(JSON.stringify(userDoc.data(), null, 2));

  const userData = {
    id: userDoc.id,
    ...userDoc.data(),
  };

  return userData;
};

multiple times, for the same userId, without having changed anything in the DB, I get the same data but with disordered fields:

{ // 1st fetch
   name: "a",
   age: 10,
   avatar: {
      uri: "x",
      thumbnailUri: "y"
   }
}


....


{ // 2nd fetch
   name: "a",
   avatar: {
      thumbnailUri: "y",
      uri: "x"
   },
   age: 10
}

What is the reason behind this? Is it possible to always get an ordered version?

1 Answers

Firestore documents contain data in a JSON format, and there is no way of guaranteeing the order of fields in a JSON object. If you need your fields ordered, you'll have to do it on the client.

Please see this post for more information

Related