Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点无法';t在mongodb中保存数据_Javascript_Node.js_Mongodb_Express_Pug - Fatal编程技术网

Javascript 节点无法';t在mongodb中保存数据

Javascript 节点无法';t在mongodb中保存数据,javascript,node.js,mongodb,express,pug,Javascript,Node.js,Mongodb,Express,Pug,我在节点9.4.0上。我试图学习youtube上的教程-Traversy media()。我在教程中做了所有的事情来解析帕格制作的html表单的数据,并将表单数据保存在mongoDB表中。这就是我所做的 //initailize const express = require('express'); const path = require('path'); const mongoose = require('mongoose'); const bodyParser = require('bod

我在节点9.4.0上。我试图学习youtube上的教程-Traversy media()。我在教程中做了所有的事情来解析帕格制作的html表单的数据,并将表单数据保存在mongoDB表中。这就是我所做的

//initailize
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');

mongoose.connect('mongodb://localhost/nodekb');
let db = mongoose.connection;

*********************************************
//check connection
db.once('open', function(){
    console.log('Connected to Mongodb');
});

//check for db error
db.on('error',function(err){
    console.log(err);
});

//init app
const app = express();

// bring in models
let Article=require('./models/article');


//load view engine
app.set('views',path.join(__dirname,'views'));
app.set('view engine','pug');
//app.set('view engine','php');
//
//Body parser
app.set(bodyParser.urlencoded({extended: false}));
//parse application/json
app.use(bodyParser.json());


//home route
app.get('/',function(req,res){

Article.find({},function(err,articles){
    if(err){
        console.log('error');
    }
    else
    {
        res.render('index.pug',{
            title:'articles',
            allarticles: articles
        });
    }
})


    //res.send('Hello world');
});

//a route to another resource
app.get('/articles/add',function(req,res){
    res.render('add_article.pug',{
        title:'Add articles'
    });
});

// Add submit POST route
app.post('/articles/add',function(req,res){
    let article = new Article();
    article.title = req.body.title;//we need to install and include body-parser to parse form html body
    article.author = req.body.author;
    article.body = req.body.body;

    article.save(function(err){
        if(err){
            console.log(err);
            return;
        }
        else {
            res.redirect('/');
        }
    });

    //console.log('Submitted');
    //return;
});


app.listen('3000',function(){
    console.log('server started on port 3000');
});

每次我填写数据并提交时,数据库都会增加一条空白记录。由于某种原因,数据没有到达数据库,但实际上增加了一条空白记录

可能是客户端代码错误-但是。。。您是否尝试过对您发布的代码进行任何调试,例如,
console.log(article.title)
等,以查看服务器正在“接收”的内容。我刚刚检查了日志,即console.log(article.title)返回未定义的内容。这意味着pug文件或文章存在问题。title=req.body.title;确实,请检查浏览器开发人员工具控制台/网络以查看浏览器正在发送的内容
extends layout

doctype html
html
    head
        title node app with express framework
    body
            h2 #{title}

            form(method='POST', action='/articles/add')
                #form-group
                    label Title:
                    input.form-control(name='title', type='text')
                #form-group
                    label Author:
                    input.form-control(name='author', type='text')
                #form-group
                    label Body:
                    textarea.form-control(name='body')
                input.btn.btn-primary(type='submit', value='submit')