Skip to main content

How to Save Form Data in Mongodb Database using Node.js

  In this tutorial we will learn how to save data from the Form in Mongodb database using a Node.js app. We will create a simple form in html in front end and use node.js as backend server to post or save the data into mangoose database. In the previous blog post, we just learned how to submit the form data using javascript, but in this blog we will learn a little advanced concept of saving the form data into a database. To learn about how to save data from a form in Mongodb database using node.js, you need to have some basic understanding of node.js, mongoose, and html. 1. Creating a Node.js app Open the command terminal on your computer and type below commands in it. mkdir sample-app cd sample-app npm init -y   2. Install Dependencies In order to successfully run our application, we need to install some dependencies as mentioned below: Mongoose: Mongoose is a Object Data Modelling library for node.js. It helps in object mapping, creating collections, saving and retrievi

Data Types in Javascript

In any programming language, there are variables and variables store values. These values can be of different types.

In Javascript there are 8 different types of data types. Data types define the type of data that can be stored in the variable and used in the program. These 8 different data types are classified into two types primitive and non-primitive data types.

Primitive data types: These are basic data types that represent a single value. The following are the primitive data types in JavaScript:

·         Number

·         String

·         BigInt

·         Boolean

·         Undefined

·         Null

·         Symbol (added in ECMAScript 6) 

Non-primitive data types: These are complex data types that represent a collection of values. The following is the non-primitive data type in JavaScript:

·         Object: including arrays, functions, and more.

 The main difference between primitive and non-primitive data types is that primitive data types are immutable (i.e., their values cannot be changed), while non-primitive data types are mutable (i.e., their values can be changed). For example, when you assign a new value to a primitive data type, a new memory location is created to store the new value, while when you change a property of an object, the same memory location is updated with the new value.

 

The following are different data types in Javascript:

1. Number

A Number data type implies both positive or negative numbers as well as floating point numbers. It is also used to represent numbers in exponential form like 2.3e-7 (equivalent to 2.3x10-7).


Some special numerical values like Infinity, -Infinity, NaN are also included in the Number data type.

We get Infinity value which is a number greater than zero when we divide any number with zero.

alert(12/0); // infinity

alert(-12/0); // infinity

alert(12/-0); // infinity

NaN ( Not a Number) is a special value that is obtained due to a undefined mathematical operation.

alert (“NaN”/6) //NaN


2. String

String data type represents textual data or string of characters that are enclosed within single or double quotes. For example “Apple”, ‘Orange’ etc. Single or double quotes are not different and indicate same thing.


3. Boolean

Boolean data type can only contain two values i.e. either True or False.

True means ‘Yes’ and False means ‘No’.

You can also get Boolean values as a result of some comparison.


4. Null

Null data type in javascript literally represents “nothing”. However, if  you use typeof operator for null, it returns an object type. This is considered a bug in javascipt.


5. Undefined

The Undefined data type is also stand alone data type like Null and it actually means “value is not assigned”. When you just declare a variable but do not assign any value to it, then it has undefined value.


6. BigInt

The Number data type in Javascript cannot properly store integer values greater than (253 + 1) and less than -(253 - 1). If we try to store value greater than these values then a error gets displayed. To accommodate integers of arbitrary length, we use BigInt type.

Syntax

BigInt ( number)

OR

By appending ‘n’ at the end of the number.

Const biggInt = 023049823904120980948302984320948n;


7. Symbol

The Symbol data type which was newly introduced in ES6 is used to create a unique identifier key that cannot be changed.

We use Symbol() to create a new primitive symbol data type.


Two Symbols with same description are Unique

Two symbols with same description are not same even they look same.

For example:

let x1 = Symbol(“hi”);

let x2 = Symbol (“hi”);

console.log(x1 === x2); //false

Add Symbol as an Object Key

You can also add Symbol as an Object Key as shown below

You can add symbols as a key in an object using square brackets []. For example,

 

let rollno = Symbol("rollno");

let person = {

    name: "Nikhil",

    // adding symbol as a key

    [rollno]: 444 // not "rollno": 444

};

console.log(person); // {name: "Nikhil", Symbol(rollno): 123}

 The best thing about Symbol is that they are unique and immutable. You can use the same unique Symbol key in another code without duplication issues.

8. Object

The Object data type is a non-primitive data type in Javascript that is used to store large collection of data of different data types in key-value pairs. These key value pairs of objects are called as properties of objects. The values of the keys can be another objects thus making it possible to create complex nested structure.

The syntax to declare object is:

const object_name = {

   key1: value1,

   key2: value2

}

const student = { 
    name: ‘Nikhil’,
    maths: 55, 
    science: 20, 
};
 

// accessing property

console.log(student.name); // Nikhil
console.log(student[“maths”]); // 55
 

 

You can easily access the value of the key in properties using a (.) dot notation or square bracket [] notation.


We will further explore more concepts of Javascript in future blogs!

Till then Happy Coding!😀


Comments

Popular posts from this blog

API Documentation Overview

 In this blog, we will explore an example of API documentation for one example function findNeedles(). findNeedles() Overview The purpose of the findNeedles() API takes two string parameters i.e. needles array and haystack and finds the total number of occurrences of each element in the needles array that are present in the haystack string. This function searches for only five words or less from the needles array and logs an error if more words are available in the needles array. The comparison is done by first splitting the haystack string using literals like backspace, tab space, single quote, and double quote etc into single elements and then looping over the needles array to find occurrences of these elements. Parameters This function takes following parameters: Name Type Description haystack String A string of any length needles String Array An array of words     Sample Code

Understanding Intents in Dialogflow

Dialogflow is a highly robust and dynamic natural language understanding platform that allows you to quickly build highly scalable conversational chatbots by leveraging the power of Artificial Intelligence (AI). You can easily create highly customizable chatbots for variety of channels like web applications, mobile applications, Facebook page, social media apps and much more. It provides seamless integration with variety of channels like Facebook, Viber, Telegram, Slack and much more. But in order to start working on building a chatbot yourself, lets connect the dots to get a clear picture of entire chatbot development process in Dialogflow. To create a chatbot, let us understand some of the nuances of the Dialogflow. What are Intents? Intents help Dialogflow ascertain the real intent or motive of the user messages. You can provide a set of certain training phrases that can help Dialogflow get trained to recognize similar such phrases and trigger a specific intent whenever such

Using Entities in Dialogflow to Extract Vital Information

Having learned about Intents in our previous blog , in this blog we will shed light on another interesting topic of Entities in Dialogflow . Entities provide an efficient way to capture and extract crucial information like name, country, location and any custom information from user inputs. Entities can be mainly categorized into three types: 1.  System Entities : These are build-in entities provided by Dialogflow through which you can identify and extract most common type of data like name, country, date, email address etc. 2. Custom Entities : These are user-defined entities that are useful to extract custom data from the user inputs. For example, you can define a Fruit entity type that can identify and map different fruit types like Mango, Apple etc.        3.  Session Entities : As the name suggests, these entities remain active only for the session for which they have been created. They provide additional capability through which we can update custom entity types with new