【发布时间】:2017-10-22 00:32:24
【问题描述】:
我的路由器文件中有一个 post 方法,我从 Contact 模型中实例化了一个对象。但是我对 Contact 模型没有定义。错误是
错误:
**TypeError: Cannot read property 'firstname' of undefined**
routes/contact.js
var express = require('express');
var router = express.Router();
var Contact = require("../models/contacts");
router.post('/contact', function(req, res, next) {
res.send("POST method");
newContact = new Contact({
firstname: req.body.firstname,
lastname: req.body.lastname
}
models/contacts.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var contactSchema = new Schema({
firstname: String,
lastname: String
});
var Contact = module.exports = mongoose.model('Contact', contactSchema);
【问题讨论】:
标签: node.js express mongoose mean-stack