Sam
Sam

Reputation: 67

Inserting data into MongoDB using Angular JS

I am trying to insert some user input data to database, instead of mySQL i have decided to use MongoDB. here i have my input form

form.html

<div class="row" ng-controller="formController">

<form class="form-inline">
    <input class="form-control" name="userName" placeholder="Name"><br>
    <input class="form-control" name="email" placeholder="E-mail"><br>
    <input class="form-control" name="phone" placeholder="Phone"><br>
    <input class="form-control" name="address" Placeholder="Address">
    <input type="submit" class="btn btn-primary" value="Add">
</form>

</div>  

how can i insert these values into MongoDB using angular ? Note: formController is empty and has no codes in it !

Upvotes: 2

Views: 2836

Answers (3)

Sudhir Kaushik
Sudhir Kaushik

Reputation: 166

This is the only part I hate about Stackoverflow. People here consider that you should know everything before posting question.

Considering that you are new to Angular(like I was once), lets get some points cleared:

  1. Angular is a front-end framework, it will manage how data looks in HTML

  2. To make your app interact with MongoDB, you need nodejs or any other backend.

  3. You need to create a RESTful API. Hitting direct queries on DB is really not a standard for modern web-apps. Checkout this example: https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd

  4. When done creating RESTful api, you need to hit that api using GET method in angualar.

    You controller might look like this:

    var app = angular.module('myapp');
    .controller('lister', ['$http', function($http) {
    var self = this;
    
    $http.get('http://localhost:3000/testCollection')
      .then(function(response) {
    
    self.items = response.data;
    }, function(errResponse) {
    console.error('Error while fetching menus' + errResponse);
    });
    }]);
    

Upvotes: 0

ipinak
ipinak

Reputation: 6029

Accessing MongoDB is not so different than MySQL. You need some sort of library that will help you connect to it (like you would do in all SQL databases that I know). For that you might need a backend to handle the form submission, as @cbass mentioned.

Also take a look at a few restful approaches https://docs.mongodb.org/ecosystem/tools/http-interfaces/

I don't know any PHP so I cannot help with that. But take a look at this: https://secure.php.net/manual/en/class.mongodb.php

Upvotes: 1

errata
errata

Reputation: 26972

Since Mongo does not have a vanilla http interface you will need a backend adapter such as Mongoose for Node, or ActiveRecord w/prostrgres (Rails)

There are "databases", ElasticSearch comes to mind, that have a Restful JSON interface. ElasticSearch is not what I would consider a System of Record Database, but I am trying to make a point that you need an http/json layer to accept your requests and push the data into a database.

If you know JS well enough definitely give Mongoose/Express/Node a try. For any real application you will want a proper backend and Node is an easy place to start.

Upvotes: 0

Related Questions