【发布时间】:2015-04-23 02:10:54
【问题描述】:
我有一个 REST API 和一个 MEAN 应用程序。用户可以使用他们的电子邮件地址登录我的应用程序,然后使用 html 表单将项目写入数据库。
我想要实现的是:当用户编写项目时到数据库,html表单应该填充我数据库中的数据。想要的数组应该由用户的电子邮件地址标识。
所以我们得到了我的 html 表单:
<div class="form-group">
<label for="author">Author</label>
<input type="text" class="form-control" name="author" id="email" value="<%= user.local.email %>" readonly ng-value="email">
</div>
<div class="form-group">
<label for="name">Projektname</label>
<input type="text" class="form-control" name="name" ng-value="name">
</div>
<div class="form-group">
<label for="description">Beschreibung</label>
<input type="text" class="form-control" name="description" ng-value="description">
</div>
<div class="form-group">
<label for="tags">Tags</label>
<input type="text" class="form-control" name="tags" ng-value="tags">
</div>
我的 Mongoose 架构:
var mongoose = require ('mongoose');
var projectSchema = mongoose.Schema({
author : String,
name : String,
description : String,
tags : String,
media : {data: Buffer, contentType: String},
updated_at : {type: Date, default: Date.now },
active : Boolean
});
我的路线:
//GET /projects/:id
app.get('/projects_view/:id', function (req, res, next) {
Project.findById(req.params.id, function (err, project) {
if (err) return next (err);
res.json(project);
});
});
//PUT /projects/:id
app.put('/projects_view/:id', function (req, res, next) {
Project.findByIdAndUpdate(req.params.id, req.body, function (err, post) {
if (err) return next (err);
res.json(post);
});
});
我的解决方法:
<script>
var app = angular.module('myApp', []);
app.controller("formController", ['$scope', '$http', '$filter', function($scope, $http, $filter){
$http.get('/projects_view/').
then(function(res){
var allProjects = res.data;
var singleProject = $filter('filter')(allProjects, function(d){
return d.author === email;
});
};
});
}]);
</script>
我正在尝试使用$find,但它似乎对我不起作用。
有人知道我如何实现这一目标吗?
来自德国的亲切问候,
大卫
【问题讨论】:
-
更具体地说:我的目标是进行猫鼬查询,找到由特定电子邮件地址编写的项目。地址必须从视图传递到路由器。如果该电子邮件地址有数据库条目,我的回复应该以视图的 html 形式抛出
标签: angularjs node.js rest mean-stack