Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Can't use default function parameters in model.methods.set(name, function) #1602

Open
sulemanelahi opened this issue May 12, 2023 · 4 comments

Comments

@sulemanelahi
Copy link

sulemanelahi commented May 12, 2023

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

Member.methods.set('Test', function (message = '') {
	console.warn(JSON.stringify(message))
	console.warn(JSON.stringify(typeof message))
	console.warn(JSON.stringify(message.toString()))
})

Sandbox link

Current output and behavior:

undefiend

"function"

"(err, result) => {\n
    if (err) {\n
        reject(err);\n
    } else {\n
        resolve(result);\n
    }\n
};"

Expected output and behavior:

""

string

""

Environment:

Operating System: Windows 10 Pro
Operating System Version: 22H2
Node.js version (node -v): 18.16.0
NPM version: (npm -v): 9.5.1
Dynamoose version: 3.1.0

@ptejada
Copy link
Contributor

ptejada commented May 27, 2023

See the documentation about this method and the examples model.methods.set(name, 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.

@sulemanelahi
Copy link
Author

@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.

@ptejada
Copy link
Contributor

ptejada commented Jun 5, 2023

@sulemanelahi

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.

@sulemanelahi
Copy link
Author

@ptejada, I completely agree.

Either we need to update the documentation to provide more clarity, or we need to fix this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants