I can't for the life of me figure out what simple thing I'm doing wrong here. I've widdled the user schema down to one property, name. When i post the first one to the database all is well and it is saved. When I try to save another one with a different name I get CONFLICT. It must be something super simple I'm missing but I need an extra set of eyes to check it out.
Here's my schema (user.js)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
name: {
type: String
}
});
var User = mongoose.model('User', userSchema);
module.exports = userSchema;
Here's my my post request (index.js)
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
// db connection and models
var conn = require('./db');
var User = conn.model('User');
// middleware
app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
// POST - create a user
app.post('/api/user', function (req, res) {
console.log(req.body);
User.create(req.body, function (err, user) {
if (err) {
if (err.code === 11000) {
return res.sendStatus(409); // conflict
} else {
return res.sendStatus(500); // server error
}
}
res.sendStatus(200); // ok - user created
});
});
app.listen(3000);
console.log('Listening...');
Aucun commentaire:
Enregistrer un commentaire