ACES
ACES

Reputation: 490

_id is pushed to the array instead of object

My setup is nodejs with typescript + mongoose + express. I run my mongoose query and I get a league document that is 2 levels deep. I need to reorder populated array ties that is on my first level based on certain criteria and I execute the following code

                        const completed = [];
                        const upcoming = [];

                        league.ties.forEach((tie, index) => {
                            if (tie) {
                                tie?.matchStatus === "COMPLETED"
                                    ? completed.push(tie)
                                    : upcoming.push(tie);
                            }
                        });

                        upcoming.sort((a, b) => (a.startDate > b.startDate) ? 1 : ((b.startDate > a.startDate) ? -1 : 0));
                        completed.sort((a, b) => (b.startDate > a.startDate) ? 1 : ((a.startDate > b.startDate) ? -1 : 0));

                        const ties = [...upcoming, ...completed];
                        console.log(ties);

                        league.ties = ties;

                        console.log(league.ties);

On the first console.log ties is array of objects, but in the second console.log league.ties is array of _ids (strings).

Does anybody know why is this happening here and why the object is not pushed?

I tried for loops, map, lodash... I temporarily moved this code to the client Angular app and it works perfectly.

Thanks

Upvotes: 0

Views: 43

Answers (1)

kampung-tech
kampung-tech

Reputation: 386

My guess is that the league.ties property has been defined as writeable: false (read-only). When strict mode is off, it won't throw any error, but the value will not be changed also.

You can take a look at here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

Upvotes: 1

Related Questions