我收到此错误消息:
[![error message][1]][1]
http://i.stack.imgur.com/Okcb7.jpg
这些是我app.js文件的内容
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/emedb');
var Schema = mongoose.Schema;
// create a schema
var userSchema = new Schema({
name: { type: String, required: true,},
email: { type: String, required: true,},
password: { type: String, required: true },
location: String,
age: Number,
created_at: Date,
updated_at: Date
});
// on every save, add the date
userSchema.pre('save', function(next) {
// get the current date
var currentDate = new Date();
// change the updated_at field to current date
this.updated_at = currentDate;
// if created_at doesn't exist, add to that field
if (!this.created_at)
this.created_at = currentDate;
next();
});
var User = mongoose.model('emedb', userSchema);
// post to db
app.post('/newBen', function(req,res){
new user({
name: req.body.name,
email: req.body.email,
password: req.body.password,
location: req.body.location,
age: req.body.age,
created_at: req.currentDate,
updated_at: req.currentDate
}).save(function(err, doc){
if(err)res.json(err);
else res.send('Thank you for joining');
});res.end();
});
module.exports = User;
另外还有另外70行我觉得这里发帖有点太多了。还有我的表格,这是一个非常标准的玉文件。如果需要,我会发布它。它有一个名称和一个类,方法是post,动作是/ new。
这是我在mongodb的第一次尝试,我觉得我非常接近它。或者我在几英里之外?我尝试找到教程,大部分代码来自一个,但我怀疑它可能有几年了。
答案 0 :(得分:0)
请添加此行。
您尚未创建服务器实例
var express = require('express');
var app = express();