jose
jose

Reputation: 1054

How to store table with particular column should stored in array and retrieve it in Angularjs (Mean Stack)

all

Iam using Mean stack angularjs and mongodb now i have form like get the person information it's stored normal like table.my need is i have fields like this

Html code

<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>

    <script src="Controller/MongooseCrud.js"></script>
<title>Page Title</title>
</head>
<body>


    Name <input type="text" ng-model="user.name" > </br></br>
    Details</br></br>

    age:<input type="text"  ng-model="user.age"> </br></br>
     address:   <input type="text"  ng-model="user.address"> </br></br>
     Contact no:<input type="text" ng-model="user.number" > </br></br>

     <input type=button value=submit ng-click="Add()">

</body>
</html>

Controller Code

   $scope.Add = function () {
        $http.post('/AddNewuser', $scope.user).success(function (response) {
            refresh();
        });
    };

Server Code

app.post('/AddNewuser', function (req, res) {

    console.log(req.body);
    db.Users.insert(req.body, function (err, docs) {

        res.json(docs);
    });
});

it's store like table value's i want stored array inside the column

enter image description here

Upvotes: 0

Views: 169

Answers (1)

Shaishab Roy
Shaishab Roy

Reputation: 16805

If you have no schema then you can try it

app.post('/AddNewuser', function (req, res) {

    console.log(req.body);
    var newUser = {
            name: req.body.name,
            details:{
                age: req.body.age,
                address: req.body.address,
                contactNo: req.body.contactNo
            }
       };
    db.Users.save(newUser , function (err, doc) {
        if(err) {
            console.log(err);
            return res.status(500).send('Error occurred');
        }
        return res.status(200).json(doc);
    });
});

N.B: Here I created details as a object instead of array

Upvotes: 1

Related Questions