[QUESTION] v6 Migration - Mongoose array keys are string instead of number
See original GitHub issueSince 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:
- Created 3 years ago
- Comments:16
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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).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.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).
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 😉
Your welcome 😉