Posts

JavaScript check string contains in string or not using search() method

Image
JS find string contains in string If you want to check whether substring present or not in javascript then following are methods you can use: JavaScript Search() method to check whether the string contains substring is there or not const str = "this is main string to check" ; const subStr = "main string" ; if (str.search(subStr)) { console.log( ` ${ subStr } string is present in ${ str } ` ); } else { console.log( ` ${ subStr } string is not present in ${ str } ` ); } The Output will be as follows: Definition of Search() method The search() method searches a string for a specified value and returns the position of the match . Search()  method returns -1 if no match is found . We can search on a string or on a regular expression. It will return the first occurrence of the specified value. I will post more method in upcoming posts which I will link over there so you can check it. Thanks.

tilde (~) and a caret (^) in a npm package.json file

Image
What is tilde (~) and a caret (^) in an npm package.json file? If you are using npm to manage your app, then you know that we will use package.json for managing dependencies. sample package.json file So as we can see in the above sample package.json file we have some dependencies & dev dependencies as well, now let's go close to dependencies and try to understand the meaning of tilde & caret symbols. { "devDependencies": { "@angular/common": "5.0.0", "cordova-plugin-splashscreen": "^5.0.3", }, "devDependencies": { "@ionic/app-scripts": "3.2.4", "@types/chart.js": "^2.7.37", "typescript": "~2.6.2" } } As we can see above JSON from the package.json file,  key is the name of the package and the value is the version of the package to be used. The version number is in semver syntax which designates each s...

Javascript detect connection speed

Image
JavaScript detect connection speed Ever feel like your internet is running slow? Webpages seem sluggish while uploads and downloads take forever! It's very frustrating, especially when you pay for high-speed internet. As a developer, we need to find the current speed and load the pages accordingly. So if you want to get the connection speed in JS you can use the  Navigator.connection  object. Let's take an example: navigator.connection.addEventListener( 'change' , () => { let currRtt = navigator.connection.rtt; let effType = navigator.connection.effectiveType; console.log( 'current connections' , currRtt, effType) }) so the above example will trigger an event whenever there will be some changes in the network. So let's test it by changing the network connections. As you can see in logs whenever I changed the network type from network tab events gets fired and it printed the current connections types. Javascript detect...

Remove property from Javascript Object

Image
How to remove property from a JavaScript object If you want to remove property from a JavaScript object then you can do by following ways. Remove JavaScript Object Property lets create one object so we can run and test it. var user = { "id": "1001", "name": "Vikas", "email": "test@test.com", "password": "TesTP2ss", "createdAt": "20-08-2019" }; The  will be as follows: Now we can remove the object property from the following ways: Using delete operator: as we have seen the user's object now before sending to frontend we will delete the password property and then we will send it. delete user.password; >>true Or alternatively, you can do the following way as well. delete user['password']; >>true If delete won't work then you can use the following: user . password = undefined ; Please le...

Ionic ios build fails, error archive not found : Error code 65 for command: xcodebuild with args

If you are working on ionic app, and you are trying to create iOS build then usually we use the following command: ionic cordova build ios OR ionic cordova build ios --device --prod --release while creating build if you get following error: ✖ cordova build ios - failed! [ ERROR ] An error occurred while running cordova build ios (exit code 1):          error: archive not found at path '/Users/work/MyApp/platforms/ios/MyAppp.xcarchive'         ** EXPORT FAILED **         CordovaError: Promise rejected with non-error: 'Error code 65 for command: xcodebuild with args:   Then You have to use the following command: ionic cordova build ios -- --buildFlag="-UseModernBuildSystem=0" if this won't work then you can update and or create a build.json file. { "ios" : { "release" : { ..., "buildFlag" : [ ...

Mastering in Javascript | How do JavaScript closures work?

Image
How do JavaScript closures work? When I started learning some advanced concepts of JavaScript that time I got to know about closures but when tried to understand it but it was like leave me alone. Then I did some research on it but still, I was getting only theoretical concepts of closures. So I thought might be I am not alone who don't understand closures so I tried to explain it with examples. What is Closure in JavaScript? A closure is a function having access to the parent scope, even after the parent function has closed.    A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was first declared), be assigned to a variable, be passed as an argument to a function, or be returned as a function result. this is the terminology used to explain the closure but I know how hard it is to understand, so let's go by examples. Example of Closures in JavaScript function ...

MongoDB remove unique constraint

Image
MongoDB Remove the unique constraint: Src: https://en.wikipedia.org/wiki/MongoDB Today, I'm working on a RESTful API Using Node.js With Express and Mongoose example, and I ran into a problem with the MongoDB Schema: POST : { username: 'vikaskad' , email: 'testmail@test.com' , name : 'Vikas Kad' } { [ MongoError : E11000 duplicate key error index : ecomm_database . users . $email_1 dup key : { : "testmail@test.com" }] name : 'MongoError' , err : 'E11000 duplicate key error index: ecomm_database.users.$email_1 dup key: { : "testmail@test.com " }' , and my schema is as follows: var Product = new Schema ({ username : { type : String , required : true }, email : { type : String ,unique:true, required : true }, name : { type : String }, createdAt : { type : Date , default : Date . now } }); but after some database inser...