[英]How to submit a successful post request using the MEAN stack?
I'm using yeoman to scaffold a project. 我正在使用yeoman搭建项目。 In the end I want to learn to scaffold the CRUD.
最后,我想学习搭建CRUD。 I'm getting stuck on a post request.
我被张贴请求卡住了。 I've picked the angular-fullstack generator because I'm very comfortable with normal angular-generator.
我选择了角全堆栈生成器,因为我对普通角生成器非常满意。
My post request attempts are being hulled by a 400 error while trying to submit a new object to the Things collection. 尝试将新对象提交到Things集合时,我的发布请求尝试被400错误所包围。 I'm pretty lost, I need to see completed code of a post request using this folder structure.
我很迷路,我需要使用此文件夹结构查看发布请求的完整代码。
UPDATE: The 400 error is gone however, the req.body. 更新:但是,req.body出现了400错误。
UPDATE2: Everything is properly working now answers are both in my code and answer. UPDATE2:现在一切都正常工作,答案都在我的代码和答案中。
So, Using the angular-fullstack generator of yeoman, how would I make a post request to create an awesomeThing in the Thing collection of the mongo db. 因此, 使用yeoman的angular-fullstack生成器,我将如何发出请求以在mongo db的Thing集合中创建awesomeThing。
I'm surely missing something but I think most of the pieces needed to make a POST request successful are presented below. 我肯定会遗漏一些东西,但是我认为成功完成POST请求所需的大部分内容如下。 Any direction would be much appreciated.
任何方向将不胜感激。
app/views/partials/main.html 应用程序/视图/分音/ main.html中
<div class="row marketing">
<div ng-repeat="thing in awesomeThings">
<h4>{{thing.name}}</h4>
<p>{{thing.info}}</p>
</div>
</div>
<form ng-submit="addAwesome()">
<input type="text" ng-model="tempAwesome.name">
<input type="submit" class="btn btn-primary" value="Add">
</form>
app/scripts/controllers/main.html 应用程序/脚本/控制器/ main.html中
$scope.name = [];
$http.get('/api/awesomeThings').success(function(awesomeThings) {
$scope.awesomeThings = awesomeThings;
});
$scope.addAwesome = function() {
$http.post('/api/awesomeThings', $scope.tempAwesome).success(function(postData) {
$scope.awesomeThings = postData;
}).error(function(postData, status){
console.log(postData);
$scope.status = status;
console.log(status);
});
};
lib/routes.js LIB / routes.js
app.route('/api/awesomeThings')
.get(api.awesomeThings)
.post(api.create);
lib/controllers/api.js LIB /控制器/ api.js
mongoose.model('Thing', ThingSchema);
exports.create = function (req, res) {
var awesomeThing = new Thing(req.body);
awesomeThing.save(function(err) {
if (err) return res.json(400, err);
});
};
lib/models/thing.js LIB /模型/ thing.js
var ThingSchema = new Schema({
name: String,
info: String,
awesomeness: Number
});
I have a hunch it is mongoose blocking this because my submission isnt matching the schema? 我直觉这是猫鼬阻止了此操作,因为我的提交与架构不匹配? ?
? any other thoughts?
还有其他想法吗?
Edit 编辑
Ok one last suggestion based on looking at some other code I have. 好吧,最后的建议是基于查看我拥有的其他代码。 try this for your post instead
试试这个代替你的帖子
$http.post('/api/awesomeThings' { name : $scope.name })
Edit based on code change 根据代码更改进行编辑
Change your ng-model in your form to. 将表单中的ng-model更改为。
<input type="text" ng-model="name">
I think this line is failing 我认为这条线失败了
var awesomeThing = new Thing(req.body);
what I think is happening is you are sending an object that looks like this in the body 我认为正在发生的事情是您正在发送一个看起来像这样的物体
{ tempThing: "sometext" }
when mongoose tries to create your object it dosen't have a property called tempThing so it bails out. 当猫鼬尝试创建您的对象时,它没有名为tempThing的属性,因此无法使用。 This can be overridden by setting {strict:false} on your schema if you like but, if your intention is to set name from your form I would start with change tempThing to name.
如果愿意,可以通过在架构上设置{strict:false}来覆盖此设置,但是,如果您打算从表单中设置名称,那么我首先将tempThing更改为name。
Old Answer 旧答案
I think your /lib/routes.js has a typo. 我认为您的/lib/routes.js有错字。 you have a ';'
你有一个 ';' after .get(api.awesomeThings)
在.get(api.awesomeThings)之后
that's causing the .post(api.awesomeThings) to be ignored. 这导致.post(api.awesomeThings)被忽略。 remove the semicolon and it would probably start working.
删除分号,它可能会开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.