I am using loopback3 api for an app to create new users. My remote-method '/newUser' execs a stored procedure, but is failing to create the new user.
App Flow: create new user by running 3 stored procedures: usp_CreateUser
, which returns the UserID
via @UserIdOutputParam
. usp_CreatePhone
and usp_CreateEmail
are both executed from within usp_CreateUser
, and use the @UserIdOutputParam
as the FK to create the linked phone and email.
The problem: running 'newUser' remote method in loopback, which executes the usp_CreateUser
, the @UserIdOutputParam
is not able to get passed back into the usp_CreateUser
, and the code errors out there/new user is not created.
Tried hardcoding the UserIdOutputParam
to be NULL, tried omitting it.
I reviewed node-mssql & tediousjs parameters examples, but don't see how to use them with my loopback remote-method.
node-mssql:
const request = new sql.Request()
request.input('input_parameter', sql.Int, value)
request.output('output_parameter', sql.Int)
request.execute('procedure_name', (err, result) => {
tedious:
request.addOutputParameter('number', TYPES.Int);
request.addOutputParameter('string', TYPES.VarChar);
Here's my code
'use strict';
module.exports = function (Person) {
var server = require('../../server/server');
var ds = server.dataSources.MSSQL_DB;
Person.newUser = async function (
firstname,
lastname,
phonenumber,
emailaddress,
// useridoutputparam,
request
) {
request = ("EXEC dbo.usp_CreateUser" +
"@FirstName='" + firstname + "', "
+ "@LastName='" + lastname +
+ "@PhoneNumber=" + phonenumber + ", "
+ "@EmailAddress=" + emailaddress + ", "
+ "@UserIdOutputParam=" + null
+ ";");
ds.connector.execute(request, function (err, data) {
console.log(request);
if (err) {
console.log(err);
} else {
console.info("data", data);
return (err, data);
;
};
}
);
};
Person.remoteMethod('newUser', {
accepts: [
{ arg: 'firstname', type: 'string', required: false },
{ arg: 'lastname', type: 'string', required: false },
{ arg: 'phonenumber', type: 'number', required: false },
{ arg: 'emailaddress', type: 'string', required: false },
{ arg: 'useridoutputparam', type: 'string', required: false }
],
http: { path: '/newUser', verb: 'post' },
returns: { arg: 'data', type: 'string' };
}
);
//more info: https://loopback.io/doc/en/lb3/Remote-methods.html
};
I expect the remote method to exec the stored procedure, but it fails without the output parameter.
Error from console:
EXEC dbo.usp_CreateUser @FirstName='Buzz', @LastName='Lightyear', @EmailAddress='blightyear@allenmatkins.com', @PhoneNumber=7072738888, { RequestError: Procedure or function 'usp_CreateUser' expects parameter '@UserIdOutputParam', which was not supplied.