Monthly Archives: November 2015

Use of !! in JavaScript


Want to find out if something is false if it is a string, number, obj etc….

var x = “No”
typeof x
// string

var xBools = !!x;
//true – because variable x has a string of No, could be a string of false as well. A string is an object and that object has alpha characters
typeof xBools
// boolean

var emptyString = “”;
typeof emptyString 
// string

var emptyStringBools = !!emptyString;
//false – because the string is empty
typeof emptyStringBools 
// Boolean

var nulls = null;
typeof nulls
// object

var nullsBools = !!nulls;
typeof nullsBools
// boolean

var under;
typeof under
// undefined

var underBools = !!under;
typeof underBools
// boolean

Check to see if a variable has a value and if that value is usable.

How To: Submit Your Form Data as JSON data to be Consumed by a RESTful Service (Node.js & Express.js)

I need to obtain a JSON object that cantains all of my form data as key value pairs {name : value}.

Answer :

  • Use jQuery SerializeArray
    • This will not give you what you need right off. It will send back something like this
    • [{name: 'key', value = 'value'}]
  • Use the result from SerializeArray and run it through the custom SerializedArrayToJSON method described below
    • Pass the serialized array as a parameter to the SerializedArrayToJSON method.
      • The SerializedArrayToJSON will return a formatted javaScript object you can pass in your POST method.
      • Inside your route the req.body will contain each piece of your form data as ‘key : value’ pairs.

Example :

 var serializedArrayData= $('form').serializeArray();

 function SerializedArrayToJSON (serializedArrayData){
   //reformat formData
   var holder = [], returnData = {};

   for(var i = 0; i < serializedArrayData.length; i++){
    for(_key in serializedArrayData[i]){
   for(var j = 0; j < holder.length; j++){
     if(j % 2 == 0 ){
       returnData[""+holder[j]+""] = holder[j + 1];

   return returnData;

How to : Add custom validation to a Mongoose Model

I need to add a custom validation to my mongoose model before I save to my Mongo Database.

Answer –

  • Custom validation is declared by passing a validation function.


  • Note* Custom validation – method signature = validate(obj, [errorMsg])

var phoneValidator = [
   return /d{3}-d{3}-d{4}/.test(v); 
 "Please provide a valid phone number"

var customerSchema = new Schema(
   phone : 
     type: String, 
     validate: phoneValidator 

var Customer = mongoose.model('customer', customerSchema);