Promise.prototype.then() : JavaScript then() method and how to use it
Javascript , Requests , Uncategorized / September 25, 2019

Q:  How to use then() and how to return a promise from a function and use then() ?   A: then() is a method in the Promise prototype that returns a promise and accepts two callback functions. One function is for the success and the other is for the failure case of the promise. If both arguments are not given or are not callback functions, still then the method would not generate any errors. For example, here we create a new promise.

promise .then() exists hence it is a promise. .then() allows method chaining.   Now we will see how to use then to return a promise It is incorrect to use then() this way as it should return inside the then also. Otherwise resolving the promise returned by then will give undefined.

    It is incorrect to use the function without returning the promise returned  by then()

    Returning the promise returned by then is the correct way of returning a promise from a function

    If there is some kind of logic to be applied, this is the correct way of using then()

 

Using express-validator to validate password to have minimum 8 characters including at least one special character and at least one number

Q How to use express-validator to validate passwords to have minimum 8 characters including at least one special character at least one number? A The regular expression used to validate a password which will follow the above-mentioned pattern is as follows

While using express-validator, we use  the chain method .matches() to check the input password against regular expression as follows

The above validator checks whether the request body of the post request has a password which is a minimum of 8 characters long, and contains an alphabet and a special character.   If there are any validation errors, it can be obtained using  validationResult(req)  

To display only the first error at a time we add the following object to errors.array() 

Now handling the error part looks like this

So the final code looks like this

 

Filter array of objects having array of objects
Javascript , Requests , Typescript / March 20, 2019

Q: How to filter an array of objects having an array of objects Example object –

  A  –  When we consider the above object having an array of superheroes in members. Each superhero having an array of power. We need to get the superhero having the power 4/shield  in their power array, in this case We apply the filter function on the members array, then we apply filter function on powers array present in every item in the members array in the following way.

Gives output

On applying the filter function on inner powers array, if we get the length of the resulting array greater than 0, it means that the inner array contains the power4/shiled .  

How to add namespace to SOAP request using soap-node – Node.JS
Node.js , Requests / February 24, 2019

Q  I have XML request in the following format. How to add the XML NAMESPACE to the request JSON I’m going to send to SOAP server?

A: There are three namespaces defined in the XML. When adding them to JSON you can add them to the attributes key in the following way

   

How to set environment variables in Angular
Requests , Uncategorized / September 1, 2018

Q I have an angular application that connects to Firebase cloud. How can I set the configuration for firebase cloud as environment variables separately for production, development and testing environments? A – For creating a new Angular app using Angular CLI, we use the following command

This creates two new  environment.ts files inside Appname/src/environments folder environment.prod.ts environment.ts  .angular-cli.json in the Appname folder will have the following code

  Environment.prod.ts will be your environment file for production and environment.ts will be your default environment file. environment.ts will have the following code

The file contents for the current environment will overwrite these during a build. If you do

the environment.prod.ts will be used instead. Adding environment variables Creating environment files for development and test in src/environments folder 1 environment.test.ts

  2 environment.dev.ts

In .angular-cli.json file, add the following code

  To run using the environments, use

  Import environment  like this

 

How can I convert timestamp value to Readable date time in ng-repeat-Angular JS
Requests / August 8, 2018

Q: I have an array of data,with date stored as timestamp value.How can I convert it to readable date in html.I am using angular 1 with ionic 1 framework A:Use the following code to display date in readable format from timestamp

 

How to accurately check date range to prevent overlapping:mysql
Requests , Snippets / July 29, 2018

Q:I have a table with events , and each event has an auditorium id,start_date and end_date.In need to insert a new event in to the table without any overlapping on date range in an auditorium   select IF(count(*)>0, true, false) from events where ‘YOUR_START_DATE‘ <= end_date and ‘YOUR_END_DATE‘ >= start_date; OR select IF(count(*)>0, true, false) from events where ‘YOUR_START_DATE‘  BETWEEN start_date AND end_date or ‘YOUR_END_DATE‘ BETWEEN start_date and end_date and end_date or (‘YOUR_START_DATE‘ < start_date and ‘YOUR_END_DATE‘ > end_date); If true do not proceed with insert if false continue

 

Share this page in social media platforms