Soap call with NestJS - javascript

I have to call SOAP webservice and serve it. I am using strong-soap library(https://github.com/loopbackio/strong-soap). I get my data in the service and I can see it in console.log() but I can't send this data to my controller to serve it. I have tried using pipe(map()) and I have looked into this following topics
(https://www.freecodecamp.org/news/an-express-service-for-parallel-soap-invocation-in-under-25-lines-of-code-b7eac725702e/)
but no luck. I either get 'can't subscribe to undefined' or my request is passing without my controller getting the data and serving it.
Here is my controller.ts
export class MyController {
constructor(private readonly service: Service){}
#Get('/example')
getAll(#Query() query: Query){
return this.service.getAll(query);
}
}
and here is my service
export class Service {
private URL = process.env.URL;
constructor() { }
async getAll(query: OrderRequest) {
const request = {
req: {
req: query,
}
};
return await soap.createClient(this.URL, (err, client) => {
if (err) {
console.error(err);
} else {
let method = client.myMethodName;
method(request, (err, result, envelope, soapHeader) => {
// here I am getting the data but 'return result. is not sending it to the controller
console.log(result[0])
return result
});
}
});
}
}
As I said I have tried with map and pipe like this:
return await soap.createClient(this.URL).pipe(map((err, client) => {
if (err) {
console.error(err);
} else {
let method = client.myMethodName; // here I have an error
method(request, (err, result, envelope, soapHeader) => {
console.log(result)
});
}
}))
but I can't pass my method in the client.
Thanks

OK I have found the solution. Thing is the function above was returning the promise so solution was to initialize new promise and handle reject and resolve. Like this we get the results in resolve and take them with .then() in our controller.
Here is the code:
service.ts
getAll(query: Query) {
const request = {query: {query: query}};
return new Promise ((resolve, reject) => { // here we add new Promise
soap.createClient(this.URL, (err, client) => {
if (err) {
return reject(err); // if error reject promise
} else {
return resolve(client); // else resolve and send client
});
}
});
}
)}
controller.ts
getAll(#Query() query: query){
console.log(this.service.getAll(query))
return this.service.getAll(query).then(result=>result)
}

Related

How to return promise from fs.unlink

I want to delete a file and wait for the deletion to succeed before moving forward. I have used unlink function inside a promise to get the result, but when unlink done successfully then I am getting the result from the promise if there is any kink of error while deleting the file the promise does not return any error.
Service:
public removeUserImage(
user: User,
): Promise<NodeJS.ErrnoException | boolean> {
const pathToRemoveImage = 'src/public/uploads'+ '/' + user.image_url;
return new Promise((resolve, reject) => {
unlink(pathToRemoveImage, (error) => {
if (error) reject(error);
resolve(true);
});
});
}
Controller:
const isFileRemoved = await this._userService.removeUserImage(user);
//This block not excuting
if (!isFileRemoved) {
throw new InternalServerErrorException(
'Error occurred while trying to remove file.',
);
}
Your promise rejects if there's an error. When using await, you need to wrap the code in try..catch in order to handle any failures
try {
await this._userService.removeUserImage(user);
} catch (err) {
console.error(err);
throw new InternalServerErrorException(
'Error occurred while trying to remove file.'
);
}
FYI, you can (and should) use the Promises API versions of the fs functions
import { unlink } from "node:fs/promises";
public removeUserImage({ image_url }: User): Promise<void> {
const pathToRemoveImage = `src/public/uploads/${image_url}`;
return unlink(pathToRemoveImage);
}
If you wanted your method to always resolve with a Boolean, you'd want something like
return unlink(pathToRemoveImage)
.then(() => true) // resolve with "true" for success
.catch((err) => {
console.error("removeUserImage", image_url, err);
return false; // resolve with "false" for failure
});
The error will always go to catch block,
try {
await this._userService.removeUserImage(user);
} catch (err) {
console.error(err);
throw new InternalServerErrorException(
'Error occurred while trying to remove file.'
);
}
Suggestion: You don't need to convert unlink(callback) to promise fs has promise function also, check this
const fs = require('fs');
const fsPromises = fs.promises;
public removeUserImage(
user: User,
): Promise<void> {
const pathToRemoveImage = 'src/public/uploads'+ '/' + user.image_url;
return fsPromises.unlink(pathToRemoveImage);
}

Firebase Firestore return QuerySnapshot

I just started to play with the firebase cloud function and firestore but when I'm using firestore inside the firebase cloud function (as below code) it's return and QuerySnapshot instead of return data. If anyone has got this issue before and solved it already then tell me. It would help me to resolve this issue too.
Thanks.
export async function allRestaurants(req: Request, res: Response) {
try {
// const { id } = req.params
const restaurantsRef = admin.firestore().collection('restaurants');
const snapshot = await restaurantsRef.get();
console.log(">>>>>>>>>", snapshot);
return res.status(200).send({ data: { restaurants: snapshot } })
} catch (err) {
return handleError(res, err)
}
}
It is normal that you get a QuerySnapshot, since the get() method returns a Promise that resolves with a QuerySnapshot.
It's up to you to generate the content you want to send back to the Cloud Function consumer.
For example, you can use the forEach() method to loop over the QuerySnapshot, or, as shown below, use the docs array.
export async function allRestaurants(req: Request, res: Response) {
try {
// const { id } = req.params
const restaurantsRef = admin.firestore().collection('restaurants');
const snapshot = await restaurantsRef.get();
const responseContent = snapshot.docs.map(doc => doc.data());
return res.status(200).send({ data: { restaurants: responseContent } })
} catch (err) {
return handleError(res, err)
}
}

AngularJS testing with Jest

I am trying to mock HTTP requests for AngularJS in Jest.
I have a service like this
public UserService(user) {
let url = this.env.getUserEndpoint("/users");
let deferred = this.$q.defer<any>();
this.$http.post<{ user: IUser }>(url,user).then( (result) => {
deferred.resolve(result.data.user);
}, (error) => {
deferred.reject(this.httpErrorHandlingService.handleHttpError(error, "creating new user"));
});
return deferred.promise;
}
Test:
beforeEach(angular.mock.module("test"));
var userService;
beforeEach(angular.mock.inject(function (UserService) {
userService = UserService;
}));
it('should create new user', function () {
const newUser = {
name: "testUser",
password: "12345"
};
return userService.createNewUser(newUser).then(user => expect(user.name).toEqual("testUser")
});
The error I get is Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout
What am I doing wrong, and how can I mock AngularJS HTTP requests the easiest way?
You don't need to create your own promise - $http.post() returns one for you. Then you can return the response data in your success and error function callbacks in your then method, and these will be handed to the promise returned by $http.post(). This should simplify your service code a bit:
public UserService(user) {
public createNewUser(user) {
let url = this.env.getUserEndpoint("/users");
return this.$http.post<{ user: IUser }>(url, user).then((result) => {
return result.data.user;
}, (error) => {
this.httpErrorHandlingService.handleHttpError(error, "creating new user");
});
}
}

Angular2 change service method from callback to Async

I started a simple Angular2 Electron app, and I have a service method querying a local SQL Server database. Everything works fine so far. Now I am trying to get the results of the service DB call to my component and display it somehow.
The problem is that the query logic is written more for callback syntax:
sql.query(sqlString, (err, result) => {
...
callback(result);
...
});
I'm having a hard time rewriting it to return a promise, since the result will always be within result parameter of the query command function. My component looks like this:
export class LinkDocRetriever {
constructor(private myService: MyService) { }
results = "";
loadMyData(id: number): void {
let tcData = this.myService.getMyData();
tcData.forEach(element => {
this.results += element.FileName + " " + "....\n";
});
};
}
And my service looks like this:
import { Injectable } from "#angular/core";
import * as sql from "mssql";
#Injectable()
export class MyService {
getMyData():Array<MyItem> {
let myData:Array<MyItem> = [];
let config = {
user: "sa",
password: "xxx",
server: "localhost",
database: "mydb"
};
const pool1 = new sql.ConnectionPool(config, err => {
if (err) {
console.log("connect erro: " + err);
}
let q:string = `SELECT TOP 10 * FROM MyTable;`;
let final = pool1.request()
.query<MyItem>(q, (err, result) => {
if (err) {
console.log("request err: " + err);
}
console.log("db result count: " + result.recordsets[0].length);
result.recordsets[0].forEach(row => {
myData.push(row);
});
});
});
return myData;
}
}
I do get a result back, but the component never sees it since it comes back before the results are returned.
I've tried doing an await on the query call, within the ConnectionPool function, but I get an error stating that await can only be called within an async function, even though I have async set on that method. The mssql package has an Async/ Await section, but the given syntax on that page gives errors, when I try it.
Any idea how I can write this using a promise?
As you pointed out, there are 3 way to handle async functions: using callback, using promise, and using Async/ Await. I will try to show all three ways but you should learn about event loop in javascript and how it takes care of async functions.
Callback
Callback is technically fastest way to handle async functions but it is quite confusing at first and might create something called callback hell if not used properly. Callback hell is very terrible that someone even created a website for it http://callbackhell.com/.
So you code can be rewritten as:
export class LinkDocRetriever {
constructor(private myService: MyService) { }
results = "";
loadMyData(id: number): void {
// call getMyData with a function as argument. Typically, the function takes error as the first argument
this.myService.getMyData(function (error, tcData) {
if (error) {
// Do something
}
tcData.forEach(element => {
this.results += element.FileName + " " + "....\n";
});
});
};
}
Service
import { Injectable } from "#angular/core";
import * as sql from "mssql";
#Injectable()
export class MyService {
// Now getMyData takes a callback as an argument and returns nothing
getMyData(cb) {
let myData = [];
let config = {
user: "sa",
password: "xxx",
server: "localhost",
database: "mydb"
};
const pool1 = new sql.ConnectionPool(function(config, err) {
if (err) {
// Error occured, evoke callback
return cb(error);
}
let q:string = `SELECT TOP 10 * FROM MyTable;`;
let final = pool1.request()
.query<MyItem>(q, (err, result) => {
if (err) {
console.log("request err: " + err);
// Error occured, evoke callback
return cb(error);
}
console.log("db result count: " + result.recordsets[0].length);
result.recordsets[0].forEach(row => {
myData.push(row);
});
// Call the callback, no error occured no undefined comes first, then myData
cb(undefined, myData);
});
});
}
}
Promise
Promise is a special object that allows you to control async function and avoid callback hell because you won't have to use nested callback but only use one level then and catch function. Read more about Promise here
Component
export class LinkDocRetriever {
constructor(private myService: MyService) { }
results = "";
loadMyData(id: number): void {
this.myService.getMyData()
.then((tcData) => {
// Promise uses then function to control flow
tcData.forEach((element) => {
this.results += element.FileName + " " + "....\n";
});
})
.catch((error) => {
// Handle error here
});
};
}
Service
#Injectable()
export class MyService {
// Now getMyData doesn't take any argument at all and return a Promise
getMyData() {
let myData = [];
let config = {
user: "sa",
password: "xxx",
server: "localhost",
database: "mydb"
};
// This is what getMyData returns
return new Promise(function (resolve, reject) {
const pool1 = new sql.ConnectionPool((config, err) => {
if (err) {
// If error occurs, reject Promise
reject(err)
}
let q = `SELECT TOP 10 * FROM MyTable;`;
let final = pool1.request()
.query(q, (err, result) => {
if (err) {
// If error occurs, reject Promise
reject(err)
}
console.log("db result count: " + result.recordsets[0].length);
result.recordsets[0].forEach((row) => {
myData.push(row);
});
//
resolve(myData);
});
});
})
}
}
Async/await
Async/await was introduced to address the confusion you was having when dealing with callbacks and promises. Read more about async/await here
Component
export class LinkDocRetriever {
constructor(private myService: MyService) { }
results = "";
// Look. loadMyData now has to have async keyword before to use await. Beware, now loadMyData will return a Promise.
async loadMyData(id) {
// By using await, syntax will look very familiar now
let tcData = await this.myService.getMyData(tcData);
tcData.forEach((element) => {
this.results += element.FileName + " " + "....\n";
});
};
}
Service would be exactly the same as in Promise because Async/await was created especially to deal with them.
NOTE: I remove some Typescript feature from your code because I am more accustomed to vanilla JS but you should be able to compile them because Typescript is a superset of JS.

Propagate rejected promise via a .catch()

I have some code which essentially looks like this:
export function firstFunction(req: express.Request, res: express.Response, next: express.NextFunction): void {
secondFunction(id)
.then((userId: UserId) => {
res.status(200).send(UserId);
})
.catch((err) => {
if (err instanceof NoResultError) {
res.status(404).send(err);
} else {
next(err);
}
});
}
export function secondFunction(id: string): Promise<UserId> {
return new Promise<UserId>((resolve, reject) => {
thirdFunction(id)
.then((data: TableInfo) => {
if (Object.keys(data).length !== 3) {
reject(new Error('data in database is not mapped properly'));
}
resolve(data);
})
.catch((err) => {
// WANT TO PROPAGATE ERROR UP TO THE GETDETAILS FUNCTION WHICH CALLS THIS
});
});
}
export function thirdFunction(id: string): Promise<TableInfo> {
return new Promise<TableInfo>((resolve, reject) => {
let query = `
//query goes here
`;
db.executeQuery(query, [id])
.then((data: TableInfo) => {
if (Object.keys(data).length < 1) {
reject(new NoResultError('some message here'));
}
resolve(data);
});
});
}
My goal is to have the lowest level of the three functions (thirdFunction) determine if the data from the db-query finds no data and then reject that promise with an error. Then the secondFunction should ideally catch this error and propagate it up to firstFunction so that firstFunction can handle that error properly. I have tried doing a throw err a return err and a return Promise.reject(err) all of which lead to an unhandled promise rejection. What is my (probably fundamental) misunderstanding of how this should work?
the secondFunction should ideally catch this error and propagate it up
No, propagation is the default. Ideally you should not need to catch it at all, and it will propagate up automatically.
I have tried things that all lead to an unhandled promise rejection. What is my (probably fundamental) misunderstanding?
You're using the Promise constructor antipattern! With it, you would need to call reject(err) in the catch handler to make it work. Or really .then(resolve, reject);. But that's absolutely not how this should be done.
Instead, drop the new Promise wrapper and just return the result of chaining then handlers:
export function secondFunction(id: string): Promise<UserId> {
return thirdFunction(id)
.then((data: TableInfo) => {
if (Object.keys(data).length !== 3) {
throw new Error('data in database is not mapped properly');
}
return getUserId(data);
});
}
export function thirdFunction(id: string): Promise<TableInfo> {
let query = `/* query goes here */`;
return db.executeQuery(query, [id])
.then((data: TableInfo) => {
if (Object.keys(data).length < 1) {
throw new NoResultError('some message here');
}
return data;
});
}

Categories

Resources