如何存储 table 与特定列应该存储在数组中并在 Angularjs 中检索它(平均堆栈)
How to store table with particular column should stored in array and retrieve it in Angularjs (Mean Stack)
全部
我正在使用 Mean stack angularjs 和 mongodb 现在我有表格,比如获取个人信息,它像 table.my 一样正常存储,需要我有这样的字段
Html代码
<!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>
控制器代码
$scope.Add = function () {
$http.post('/AddNewuser', $scope.user).success(function (response) {
refresh();
});
};
服务器代码
app.post('/AddNewuser', function (req, res) {
console.log(req.body);
db.Users.insert(req.body, function (err, docs) {
res.json(docs);
});
});
它的存储方式类似于 table 值,我想在列中存储数组
如果你没有架构那么你可以试试
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:这里我创建了 details
作为对象而不是 array
全部
我正在使用 Mean stack angularjs 和 mongodb 现在我有表格,比如获取个人信息,它像 table.my 一样正常存储,需要我有这样的字段
Html代码
<!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>
控制器代码
$scope.Add = function () {
$http.post('/AddNewuser', $scope.user).success(function (response) {
refresh();
});
};
服务器代码
app.post('/AddNewuser', function (req, res) {
console.log(req.body);
db.Users.insert(req.body, function (err, docs) {
res.json(docs);
});
});
它的存储方式类似于 table 值,我想在列中存储数组
如果你没有架构那么你可以试试
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:这里我创建了 details
作为对象而不是 array