我正在 MEAN 创建CRUD操作我创建了模型文件夹,我在其中创建了 dataScehema.js 文件到定义的模型
var mongoose = require('mongoose');
var countrySchema = mongoose.Schema({
name: { type: number },
country: { type: number }
});
module.exports = mongoose.model('country', countrySchema);
在我的 index.js 中定义节点js服务器代码
var http = require('http');
var express = require('express');
var port = process.env.PORT || 8080;
var app = express();
var appRoutes = require('./routes/appRoute');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var model = require('./model/dataSchema');
mongoose.connect('mongodb://localhost/meanDb')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/',appRoutes);
http.createServer(app).listen(port);
console.log('Backend is running on port:',port);
我将为路线创建的第三个文件 appRoute.js ,
var express = require('express');
var router = express.Router();
var Country = require('../model/dataSchema');
router.post('/create', (req, res, next) => {
var newCountry = new Country({
name: req.body.name,
capital: req.body.capital
});
newCountry.save((err, country) => {
if (err)
res.status(500).json({ msg: err });
res.status(200).json({msg: country});
})
});
所以任何人,请帮助我。
答案 0 :(得分:4)
您的架构类型应为Number而不是number
var countrySchema = mongoose.Schema({
name: { type: Number },
country: { type: Number }
});