push() function shows error message: `cannot add a new property`

584 views Asked by At

This is my code:

const saveReserve = () => {
if (polygons && creatingPolygon) {
  let last = creatingPolygon.coordinates[creatingPolygon.coordinates.length - 1][0][0];
  console.log({last, polygons})
  let newline = polygons. Coordinates[0][0].push(last);
  console.log({newLine})
  let oldArray: reservaLegal = {
    coordinates: [
      polygons.coordinates[0],
      creatingPolygon.coordinates[0]
    ],
    type: "MultiPolygon",
  };
  setPolygons(oldArray);
  dispatch(saveLegalReserve(oldArray));
  setCreatingPolygon(null);
} else {
  setPolygons(creatingPolygon);
  dispatch(saveLegalReserve(creatingPolygon));
  setCreatingPolygon(null);
}

};

and when I call this function, I receive this error in the screen showing an error in the line 111

let newline = polygons. Coordinates[0][0].push(last);:

enter image description here

and this error in the console: ERROR TypeError: cannot add a new property, js engine: hermes

how can I fix that? because it doesn't make sense to me.

1

There are 1 answers

0
Adii_Mathur On

The error you're encountering is due to the fact that you're trying to add a new property to an object that is not extensible. In js, objects are usually extensible however, in some JavaScript engines like Hermes, which is used in React Native, objects may not be extensible.

if (polygons && polygons.Coordinates &&
 Array.isArray(polygons.Coordinates[0] [0])) {
  polygons.Coordinates[0][0].push(last);
  console.log(polygons);
} else {
 console.log('Cannot push to polygons.Coordinates[0][0]');
}

This code will push last to polygons.Coordinates[0][0] if it’s possible, or log an error message otherwise. This should help you identify whether the problem lies with polygons, Coordinates, or polygons.Coordinates[0][0]