question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] v6 Migration - Mongoose array keys are string instead of number

See original GitHub issue

Since migrating to v6, the mongoose array returns array keys as string instead of number.

For instance, I have the following field in my model:

@Ref(Gender)
interests: string[];

Let’s say it contains the following values (which will be valid object ids in reality): ['a', 'b', 'c']

When logging it via $log, it appears to be a normal array: ['a', 'b', 'c']

However, when sending it in a response, I’m getting the following:

{
	'0': 'a',
	'1': 'b',
	'2': 'c'
}

If I iterate over interests manually and convert the keys to numbers it solves the issue:

private convertArray(value: any[]) {
        const result: string[] = [];

        for (let key in value) {
            result[ Number(key) ] = value[key];
        }

        return result;
    }

Any idea why it happens and how it can be avoided? In v5 it used to work as expected, the array keys were numbers.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
Romakitacommented, Mar 20, 2021
  1. No, empty Returns does nothing. But give a type as follow @Returns(200, MyClass) helps the json-mapper to use the correct class for each field. But by default is not mandatory, I think with your latest feedback, json-mapper are able to infer correctly the type for the return value (and his props).

  2. Yes, the Returns decorator help json-mapper to avoid mapping mistakes by using the exact type from TypeScript metadata. The mapping is also simplified for the developer because, the mapping is based on the class and his props metadata. By using groups, Ts.ED will only map the expected props.

  3. If you don’t care about documentation, isn’t necessary. But, I encourage you to use Swagger is a very useful tool to test your API manually and to have a quick overview of your contract services 😃. There is no high cost to maintain it with the Ts.ED decorator and consumers can use it to generate client (or just test you api).

  4. Interface cannot be used to serialize object. Again, use this decorator isn’t necessary in most case. My point is, I prefer to use Returns when I return a class or a collection of class to avoid mapping mistake (but I think with your last feedback the json-mapper v6 is now stable 😃 )

So you don’t need to create class 😉

  1. Sure, here is the documentation: https://tsed.io/docs/model.html#groups
0reactions
Romakitacommented, Mar 20, 2021

Your welcome 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose v6.8.1: FAQ
A. In order to avoid executing a separate query for each document returned from the find query, Mongoose instead queries using (numDocuments *...
Read more >
Object object instead of string when pushing into array ...
1 Answer 1 ... You are facing this problem because you are trying to convert a object into string due to which a...
Read more >
mongoose/CHANGELOG.md at master - GitHub
MongoDB object modeling designed to work in an asynchronous environment. - mongoose/CHANGELOG.md at master · Automattic/mongoose.
Read more >
How to convert primitive array into object array with key and ...
Hi, i have an array like this array: [0,1,2,4,5,7,86,4,32,3,4] i need to assign a key to them all like the result will be:...
Read more >
Node.js v19.3.0 Documentation
ifError(value); assert.match(string, regexp[, message]); assert. ... Returns: <Array> with all the calls to a tracked function. Object <Object>.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found