[英]post data not being recognized node js
Here is my index.js
file... 这是我的index.js
文件...
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'QChat' });
});
router.post('/login', function(req, res) {
console.log("processing");
res.send('respond with a resource');
});
module.exports = router;
And here is the code I am using to stored POST data into my mongoDB database. 这是我用来将POST数据存储到我的mongoDB数据库中的代码。 This is located in my app.js
file... 这位于我的app.js
文件中...
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/user');
var db = mongoose.connection;
var Schema = mongoose.Schema;
db.on('error', console.error);
db.once('open', function() {
console.log("connected");
var Schema = new mongoose.Schema({
mail : String
});
var User = mongoose.model('emp', Schema);
app.post('/login', function(request, response){
console.log("here");
new User({
mail: request.body.email
}).save(function(err, doc) {
if (err)
res.json(err);
else
console.log('save user successfully...');
});
});
Code works fine up until it reaches the app.post
, after that it does not seem to read the rest of the code. 代码工作正常,直到它到达app.post
,之后它似乎没有读取其余的代码。
I know my index.js
file works because when I submit the form, I get to a page that displays respond with a resource
(because of the send function). 我知道我的index.js
文件有效,因为当我提交表单时,我会进入一个显示respond with a resource
的页面(因为send函数)。 But for some reason, app.post
is not being read, am I missing something? 但由于某种原因, app.post
没有被阅读,我错过了什么?
Here is my jade html to show that I am implementing everything correctly... 这是我的jade html,表明我正在正确实现一切......
form(class="inputs", action="/login", method="post")
input(type="text", name="email",class="form-control", id="emailLogin", placeholder="Queen's Email")
input(type="submit",name = "homePage" class ="loginButton" value="Log In" id="loginButton")
Please try to move the following code out of db.once('open')
请尝试将以下代码移出db.once('open')
db.on('error', console.error);
db.once('open', function() {});
app.post('/login', function(request, response){
console.log("here");
new User({
mail: request.body.email
}).save(function(err, doc) {
if (err)
res.json(err);
else
console.log('save user successfully...');
});
});
Another issue in your code, please make sure the first parameter of mongoose.model
is User
, otherwise, one error could pop up. 您的代码中的另一个问题是,请确保mongoose.model
的第一个参数是User
,否则可能会弹出一个错误。
var UserSchema = new mongoose.Schema({
mail : String
});
var User = mongoose.model('User', UserSchema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.