You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using model.methods.set(name, function) to define a function, I have noticed that setting default parameters does not work as expected. Instead of the default values being applied, the function default values always returns undefined. However, the typeof operator identifies the type of the variable (default paramter) as a function. Additionally, when I apply .toString() to the function, it returns the representation of an arrow function.
It seems that there is an issue with the way default parameters are handled when using model.methods.set(name, function). The default parameter values are not being assigned correctly, resulting in undefined being returned. Furthermore, the usage of .toString() on the function appears to generate the representation of an arrow function.
You can also pass parameters into your custom method. It is important to note that if you decide to pass custom parameters into your custom method, the callback parameter will always be passed in as the last parameter. This means it's highly recommended that you always pass in the same number of parameters every time to your custom method. In the event you are unable to do this (dynamic/custom parameter length), you can use the JavaScript arguments variable to retrieve the last argument that was passed into the function.
So based on the current design, it doesn't seem to make sense to have a param with a default value since is always recommended to use the number of params when calling these methods.
@ptejada, I believe the documentation text is not related to function default parameters, but rather to how the custom method handles the callback that is passed into it. It is clear that the current implementation in dynamoose does not support this functionality. However, I think it is essential to include this feature in custom methods.
Are setters with default value params supported? No and you can find why in the documentation quoted.
I am not saying I agree with how this is documented as a recommendation to always call these setters with the same number of arguments. Without following this recommendation the behavior of the handler will be unpredictable as you found out. Given the current implementation it sounds like it is a requirement to always call these helper methods with the same number of arguments. In other words, if you are always calling your helper method without params then the param with default is not even applicable.
Summary:
When using
model.methods.set(name, function)
to define a function, I have noticed that setting default parameters does not work as expected. Instead of the default values being applied, the function default values always returns undefined. However, the typeof operator identifies the type of the variable (default paramter) as a function. Additionally, when I apply .toString() to the function, it returns the representation of an arrow function.It seems that there is an issue with the way default parameters are handled when using model.methods.set(name, function). The default parameter values are not being assigned correctly, resulting in undefined being returned. Furthermore, the usage of .toString() on the function appears to generate the representation of an arrow function.
Code
Sandbox link
Current output and behavior:
Expected output and behavior:
Environment:
Operating System: Windows 10 Pro
Operating System Version: 22H2
Node.js version (
node -v
): 18.16.0NPM version: (
npm -v
): 9.5.1Dynamoose version: 3.1.0
The text was updated successfully, but these errors were encountered: