I am calling a stored procedure within an API, creating a simple registration/login system.
db.executeQueryWithParams(config.storedProcedures.CheckEmail, email, (err, result) => {
if (err) {
console.log(err);
} else {
console.log("succeeded - ", result);
;
}
});
This "ExecuteQueryWithParams" is a standard function that I have been handed that is used in conjunction with the database. I have a working version of it elsewhere in my code where I insert the user information into my database, so I know that it isn’t an issue with the function itself. For clarity, this is the function:
executeQueryWithParams(storedProc, params, callback) {
var request = this.getRequest();
for (var key in params) {
request.input(key, params[key]);
}
this.executeRequest(request, storedProc, callback);
}
This is the stored procedure that is being called:
ALTER PROCEDURE [dbo].[sp_CheckEmail]
@email varchar(255)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * from [TABLE] WHERE email = @email
END
However when the Stored Procedure is called, I am getting an error that I am passing too many arguments into the procedure. I am getting the email parameter from the API request, and I am ONLY passing in the email address, e.g. "[email protected]"
Specifically the error says "RequestError: Procedure or function sp_CheckEmail has too many arguments specified."
I am confident that I am only passing the one required argument into the stored procedure, but I cannot figure out why it is telling me that I have too many.
I have tried different formats, making sure that it wasn’t due to the email address or any special characters that may be causing the issue. Even if I just use a simple string like a name it is still giving me this issue. I have also tested by creating more arguments, up to 5, to see if that would somehow satisfy the error that I am having.
2
Answers
for...in
on a string (email
) is your problem hereYour code will be trying to ad one parameter to your stored proc for every character in your email.
Your
for
is going to loop through all the characters in the string. Instead pass in an object with a property calledemail