我使用 MEAN(Mongodb、Express、Angular、Node)编写了一个基本的待办事项应用程序。后端即。应用程序的 Node.js 部分工作正常,并已使用 postman rest api 客户端进行验证。但是我无法将从客户端获取的值正确地传递到后端,可能是我的语法不正确,我无法弄清楚。此外,我无法让那些子文档化的数组字段值在客户端和每次显示返回一个空数组字段。
它工作正常的后端部分截图: Screenshot 1 Screenshot 2
我已经将所有代码上传到 git 公共(public)存储库,您可以下载并试用完整的应用程序 git repository
或者您可以在此处检查以下 Angular Controller 、html 和其他相关后端代码。
发布方式:
api.post('/tasks', function (req, res) {
var task = {};
task.tasktype = req.body.tasktype;
//category : [req.body.category],
task.taskinfo = [];
for (var i = 0; i < req.body.taskInfo.length; i++) {
console.log(i);
var taskInfo = new TaskInfo(req.body.taskInfo[i]);
task.taskinfo.push(taskInfo);
console.log(task);
}
var taskObj = new Task(task);
taskObj.save(function (err) {
if (err) {
res.send(err);
return;
}
res.json({
message: 'Task has been created'
})
});
});
task.js 模型
var mongoose = require('mongoose');
var TaskInfoSchema = require("./taskinfo.js").TaskInfoSchema
var Schema = mongoose.Schema;
// Task schema
var taskSchema = mongoose.Schema({
tasktype : {type: String},
createdon : {type: Date, default: Date.now},
updatedon : {type: Date},
taskinfo : [TaskInfoSchema]
});
module.exports = mongoose.model('Task', taskSchema);
taskSchema.pre('save',function(next){
this.updatedon = new Date()
next();
});
taskinfo.js 模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var taskInfo = mongoose.Schema({
isactive:{type:Boolean},
taskobject:{type:String},
taskdetails:{type:String},
iscompleted:{type:Boolean}
},{ _id :true,// false
});
var TaskInfo = mongoose.model('TaskInfo', taskInfo);
module.exports.TaskInfo = TaskInfo;
module.exports.TaskInfoSchema = taskInfo;
任务 Controller :
$scope.addTask = function(){
$http.post('/api/tasks',$scope.task).success(function(response){
window.location.href='/#tasks';
});
}
添加任务 html:
<h2 class="sub-header">Tasks</h2>
<div class="table-responsive" ng-init="getTasks()">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Task Type</th>
<th>IsActive</th>
<th>Task Object</th>
<th>Task Details</th>
<th>Is Completed</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="task in tasks">
<td>{{task._id}}</td>
<td>{{task.tasktype}}</td>
<td>{{task.taskInfo.isactive}}</td>
<td>{{task.taskInfo.taskobject}}</td>
<td>{{task.taskInfo.taskdetails}}</td>
<td>{{task.taskInfo.iscompleted}}</td>
<td><a href="#/tasks/details/{{task._id}}" class="btn btn-primary">Details</a> <a href="#/tasks/edit/{{task._id}}" class="btn btn-default">Edit</a> <button ng-click="deleteTask(task._id)" class="btn btn-danger">Delete</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
最佳答案
如上所写here :
Sub-documents enjoy all the same features as normal documents. The only difference is that they are not saved individually, they are saved whenever their top-level parent document is saved.
var Parent = mongoose.model('Parent', parentSchema);
var parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
parent.children[0].name = 'Matthew';
parent.save(callback);
所以你应该改变你保存任务和任务信息模型的方式。在您的 api.js
文件中更改:
task.tasktype = req.body.tasktype;
//category : [req.body.category],
task.taskinfo = [];
for (var i = 0; i < req.body.taskInfo.length; i++) {
console.log(i);
var taskInfo = new TaskInfo(req.body.taskInfo[i]);
task.taskinfo.push(taskInfo);
console.log(task);
}
var taskObj = new Task(task);
收件人:
task.tasktype = req.body.tasktype;
var taskObj = new Task(task);
taskObj.taskinfo = req.body.taskInfo;
然后改变你的看法:
<tr ng-repeat="task in tasks">
<td>{{task._id}}</td>
<td>{{task.tasktype}}</td>
<td>{{task.taskInfo.isactive}}</td>
<td>{{task.taskInfo.taskobject}}</td>
<td>{{task.taskInfo.taskdetails}}</td>
<td>{{task.taskInfo.iscompleted}}</td>
<td><a href="#/tasks/details/{{task._id}}" class="btn btn-primary">Details</a> <a href="#/tasks/edit/{{task._id}}" class="btn btn-default">Edit</a> <button ng-click="deleteTask(task._id)" class="btn btn-danger">Delete</a></td>
</tr>
收件人:
<tr ng-repeat="task in tasks">
<td>{{task._id}}</td>
<td>{{task.tasktype}}</td>
<td>{{task.taskinfo[0].isactive}}</td>
<td>{{task.taskinfo[0].taskobject}}</td>
<td>{{task.taskinfo[0].taskdetails}}</td>
<td>{{task.taskinfo[0].iscompleted}}</td>
<td><a href="#/tasks/details/{{task._id}}" class="btn btn-primary">Details</a> <a href="#/tasks/edit/{{task._id}}" class="btn btn-default">Edit</a> <button ng-click="deleteTask(task._id)" class="btn btn-danger">Delete</a></td>
</tr>
PS:检查您的 github 存储库是否有拉取请求。
关于javascript - 如何传递子文档数组字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33944895/