0

i have 2 models in mongoose , i want to assign values in the second model to the first one .

the geometry representation in first model 1st `

geometry: {
        type: string;
        coordinates?: [[number]];
    };

`

the geometry representation in second model 2nd

geometry: {
        type: string;
        coordinates?: [type: [number]];
    };

here is the representation of my assignement as u can see we have the first model representated in left side as a started line , the second model is which one i grab data and i'm using map méthod to loop inside it.

geometry: {
                    type: client.geometry.type, 
                    coordinates: client.geometry.coordinates?.flatMap((coordinates) => coordinates)
                }

the error i received is captured in a clear image here

how i can solve this problem?

-- keyword:

number[] [[number]]

i've tried to cast value . no effect same problem

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • So from my understanding, the first model is a double array of number, and second model is a normal array number correct? If so why can't you just map `[number]` instead of `[type: [number]]` on the second model? – Luís Mestre Nov 22 '22 at 11:01

1 Answers1

1

You are trying to assign number[] to number[][] (more wide type of [[number]])

Thast is caused by Array<T[]>.flatMap() returning T[] instead of T[][] as you expect, because flatMap always flattens out one level of array

You probably need to use .map(e => e) instead, or .flatMap(e => e).map(e => [e])

Dimava
  • 7,654
  • 1
  • 9
  • 24
  • thank you for your good response. it make sens clearly. Impossible d'assigner le type 'number[]' au type '[number]' , i keep having the same error – Black-Badger Nov 22 '22 at 13:31