2

I'm trying to store an object to Firestore. My object contains an array:

{
  returnMode: false,
  id: "SE-74c5219a-acfe-4185-9e33-f78b10ac3f1e",
  prices: [
    {
      price: {
        twoHours: 0,
        firstHour: 0,
        id: "zero",
        unlock: 0,
        final: 0,
      },
      permission: { id: "GENERAL" },
    },
    {
      price: {
        twoHours: 150,
        unlock: 100,
        id: "oebb_low",
        firstHour: 50,
        final: 50,
      },
      permission: { id: "OEBB" },
    },
  ],
}

If this object is hard-coded into the Cloud Function it lets me save it to Firestore without failing. If I replace the array of the above object with a variable like so it still works:

const array = [
    {
      price: {
        twoHours: 0,
        firstHour: 0,
        id: "zero",
        unlock: 0,
        final: 0,
      },
      permission: { id: "GENERAL" },
    },
    {
      price: {
        twoHours: 150,
        unlock: 100,
        id: "oebb_low",
        firstHour: 50,
        final: 50,
      },
      permission: { id: "OEBB" },
    },
  ];
{
  returnMode: false,
  id: "SE-74c5219a-acfe-4185-9e33-f78b10ac3f1e",
  prices: array,
}

However, when the above array is not hard-coded into the Cloud Function but created by my program, it fails. This is the code that generates the array:

const bikeBoxPriceArray = [];

for (let i = 0; i < bikeBoxPermissionArray.length; i += 1) {
  const doc = bikeBoxPermissionArray[i];
  // I cut out the stuff that is not important

  const bikeBoxPriceArrayElement = {
    permission: {
      id: doc.id,
      name: doc.name,
    },
    price: {
      id: priceId,
      unlock: priceUnlock,
      firstHour: priceFirstHour,
      twoHours: priceTwoHours,
      final: priceFinal,
    },
  };

  bikeBoxPriceArray.push(bikeBoxPriceArrayElement);
}

I verified with bikeBoxPriceArray instanceof Array that it is indeed an array. What am I doing wrong? I tried out many many things but I just cannot figure it out. Why is it working when hard-coded but not with the variable?

The code is inside of a Firebase Transaction and there is no error message, it just tells me the location of the error.

2
  • Can you provide additional code so that anyone can use it to try to reproduce your error? Additionally, please attach the error message you encountered for another reference. Commented Feb 2, 2022 at 9:26
  • @RJC I already solved the problem and I did not get an error message other then that the transaction failed. I answered my own question to explain how I solved it. The problem was very strange and I believe my answer can help some people with a similar issue. Commented Feb 6, 2022 at 12:08

1 Answer 1

2

I found an answer to my own question from this post.

Before merging the object with the array the array needs to be stringified and then parsed again like so:

const array = JSON.parse(JSON.stringify(bikeBoxPriceArray));

const sessionDocObj = {
  returnMode: false,
  timestamp: {
    // sessionCreated: admin.firestore.FieldValue.serverTimestamp(),
    sessionCreated: "Karoline",
  },
  id: "SE-" + uuidv4(),
  prices: array,
};
Sign up to request clarification or add additional context in comments.

Comments

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.