【发布时间】:2018-01-11 19:17:51
【问题描述】:
客户端:html代码
<body ng-controller="myAppController">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<h1>person details</h1>
</div>
</div>
</nav>
<div>
<form name="myform" ng-submit = "save()">
<label>Enter your details</label><br>
<input type ="text" name ="name" placeholder="enter name..">
<input type ="text" name="surname" placeholder ="enter surname..."><br>
<input type ="number" name="age" placeholder ="enter age.."><br>
<input type="submit" value="submit"><br>
</form>
</div>
<div class="container-fluid text-center ">
<div class="col-sm-8 text-left" ng-repeat ="person in people">
<ul>
<li>
{{person.name}} {{person.surname}} {{person.age}}
</li>
</ul>
</div>
</div>
客户端:角度代码
var myApp = angular.module('myApp',[]);
myApp.controller('myAppController',['$scope', '$http',function($scope, $http, myAppService){
$scope.people = [];
$scope.init = function(){
$http.get('http://localhost:3000/person/name').then(function(result){
$scope.people = result.data;
});
}
$scope.init();
$scope.save = function () {
$http.post('http://localhost:3000/add').then (function() {
var formData ={name: this.name,
surname: this.surname,
age: this.age};
this.name = '';
this.surname = '';
this.age = '';
var jdata = 'mydata='+JSON.stringify(formData);
})
}
$scope.save();
}]);
服务器端 expressjs 代码,我想问题出在 Angular js 代码中,我能够保存在 factoryjs 中创建的第一个文档并显示在浏览器中,但是我输入到文本框中的内容不会保存并且给出以下错误: angular.js:11821 POST http://localhost:3000/add 500 (Internal Server Error)
var express = require('express');
//var cors = require('cors-anywhere');
var app = express();
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjId = Schema.ObjId;
var Factory = require('./model/factory.js');
mongoose.connect('mongodb://localhost/helloworld');
var db = mongoose.connection;
//adding control to make sure mongodb works fine
db.on('error', function(){
console.log("connection error");
});
db.once('open', function(){
console.log("Mongo Working")
});
var factory = new Factory(Schema, mongoose);
factory.createSchema();
factory.insertPeople();
app.get('/ping',function(req,res){
res.send('hello world this is from server!');
});
app.get('/:id', function(req, res){
res.send('hello world this is from server '+req.params.id);
});
app.post('/add', function (req, res){
console.log(req.body);
console.log(req.body.mydata);
var jsonData = JSON.parse(req.body.mydata);
console.log(jsonData.name);
console.log(jsonData.surname);
console.log(jsonData.age);
db.helloworld.save({name: jsonData.name, surname: jsonData.surname, age: jsonData.age}, function(err, saved) {
if( err || !saved ) res.end( "User not saved");
else res.end( "User saved");
});
});
app.get('/person/name', function(req, res){
var resp = factory.getPerson({name:'mani'}, res);
});
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next){
res.header('Access-Control-Allow-Origin',"*");
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/', function(req, res){
res.render('index');
})
app.listen(3000);
console.log('listening to port 3000....');
Mongoose model--factory.js 插入第一个创建的并保存
var Factory = function(Schema, mongoose){
this.Schema = Schema;
this.mongoose = mongoose;
this.createSchema = function(){
PersonSchema = new this.Schema({
name: String,
surname: String,
age: Number
});
this.Person = mongoose.model('Person', PersonSchema);
}
this.insertPeople = function(){
var mani = new this.Person({
name: 'mani',
surname: 'm*****',
age: 25
});
mani.save();
}
this.getPerson = function(query, res){
this.Person.find(query, function(err, output){
res.json(output);
})
}
}
module.exports = Factory;
【问题讨论】:
-
这里其实有很多问题。最值得注意的是
db.helloworld.save()根本无效。您正在使用猫鼬,这意味着您“应该”在这里使用模型。您似乎也不了解 mongoose 的使用模式,因为Factory模块也确实无关紧要。 Mongoose 模型通过“全局”连接注册,并可从现有的单例实例mongoose中检索。还有一个叫做body-parser 的小东西已经存在了一段时间。 -
是的,我处于学习阶段...为什么不修改代码并使其工作。(我想保存数据并在一个 html 页面上显示保存的数据)。 factory 模块与 app.post 无关。工厂模块被编写来保存第一个文档并显示它。我要做的是提交一个人的姓名,姓氏,年龄并将数据库中保存的文档显示到一个html页面中
-
如果你可以在这里真正做到这一点,那就太好了。但不幸的是,将整个程序扔给某人并询问 “请修复它” 是 StackOverflow 上的一个题外话关闭原因。我建议您开始将您的程序分解为单独的任务并开始诊断问题。请参阅How to create a Minimal, Complete, and Verifiable example,这是您自己调试的一种方便做法,也是不需要书本回答的问题的一般要求。
标签: javascript angularjs node.js express mongoose