为什么我的Mongo文档无法通过Mongoose正确保存?

时间:2014-08-22 19:27:00

标签: mongoose

//reportconfig.js    
//Load up the report model
var Report = require('../models/report');
console.log('Report ' + Report);

//expose this function to our app using module.exports
module.exports = function(req) {
    console.log('exporting' + req.body.address);
    var newReport = new Report();
    console.log('local ' + newReport.local.address);

    newReport.address = req.body.address;
    newReport.city = req.body.city;
    newReport.state = req.body.state;

    console.log('save ' + newReport.save);

    newReport.save(function(err) {
        console.log('saving ' + newReport.address);
        if(err) {
            console.log('error ' + err);
            throw err;
        }
        //return newReport;
    })
}


//server.js

//set up=======================================================
//get all the tools we need
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

var configDB = require('./config/database.js');

// configuration =============================================================================
mongoose.connect(configDB.url);//I NEED TO FIGURE OUT HOW TO CONNECT TO MY LOCAL DB

require('./config/passport')(passport);//pass passport for configuration
var report = require('./config/reportconfig');

//set up our express application
app.use(morgan('dev'));//log every request to the console
app.use(cookieParser());//read cookies (needed for auth)
app.use(bodyParser());//get information from html forms

app.set('view engine', 'ejs'); //set up ejs for templating

//required for passport
app.use(session({secret: 'ilovescotchscotchyscotchscotch'}));//session secret
app.use(passport.initialize());
app.use(passport.session());//persistent login sessions
app.use(flash());//use connect-flash for flash messages stored in session

//routes ======================================================================================
require('./routes/routes.js')(app, passport, report);// load our routes and pass in our app and fully configured passport

//launch ======================================================================================
app.listen(port);
console.log('The magic happens on port ' + port);

//routes.js
    app.post('/report', function(req, res) {
        report(req);
        console.log('res ' + res);
        res.redirect('/profile');
    });


// app/models/report.js
// load the things we need
console.log('model');
var mongoose = require('mongoose');


// define the schema for our user model
var reportSchema = mongoose.Schema({

    local : {

        address : String,
        city : String,
        state : String

    }

});


// create the model for users and expose it to our app
module.exports = mongoose.model('Report', reportSchema);

当我发布到/ report时会发生什么,调用模型并调用save。数据库获取一个新条目,但它只有_id,而不是任何其他字段。没有错误。知道我做错了什么吗?如果你问,我可以提供更多信息。上面的代码来自三个单独的文件 由于打印报告有效,因此模型正确加载。 newReport的所有字段也都有效。

2 个答案:

答案 0 :(得分:1)

您的架构中不需要本地。当您保存使用未在架构中定义的newReport.address时。如果你想保持相同的结构,它将是newReport.local.address。以下是定义mongoose模式方法的文档:http://mongoosejs.com/docs/guide.html

mongoose.model('Report', reportSchema);

var reportSchema = mongoose.Schema({

   address : String,
   city : String,
   state : String

});

答案 1 :(得分:1)

newReport.address等未在Schema中定义。因此,为了维护它,Mongoose将它们作为.save()的一部分删除。

要匹配架构,读取和分配需要使用newReport.local

newReport.local.address = req.body.address;
newReport.local.city = req.body.city;
newReport.local.state = req.body.state;
console.log('saving ' + newReport.local.address);

或者,如果没有使用,local可以从Schema中删除:

var reportSchema = mongoose.Schema({
    address : String,
    city : String,
    state : String
});