表达mongoose Mongodb没有正确插入

时间:2017-06-18 14:05:36

标签: node.js mongoose

我有以下架构。当我尝试注册一个新的user时,mongodb用新的用户替换最后一个注册用户的名字。 ObjectId对于该条目保持不变。不知道它为什么这样做。没有错误。

console.log在后​​api的任何地方编辑,一切都很好。该条目在数据库中可见,但已替换后者。问题可能是我的架构设计吗?有些东西告诉我,这是我在模式定义中创建所有内容String的方式。我和MEAN一起建设。

var AnswerSchema = Schema({
    response    : { type: String, default:null},
    question    : { type: String, ref: 'Question'},
    employee    : { type: String, ref: 'User'}
})

var QuestionSchema = Schema({
    title : String,
});

var UserSchema = Schema({
    username : String,
});

module.exports = mongoose.model('Answer', AnswerSchema);
module.exports = mongoose.model('Question', QuestionSchema);
module.exports = mongoose.model('User', UserSchema);

API

var Question = require('../models/questions'); 
var User = require('../models/users'); 
var Answer = require('../models/answers');
var jwt = require('jsonwebtoken');
var secret = "markov";
var user = new User(); 
var question = new Question();
var answer = new Answer();

// Export routes to the main server.js file
module.exports = function(router) {
    /* ====================
    User Registration Route
    ==================== */
    router.post('/users', function(req, res) {
        user.username = req.body.username;

        if (req.body.username == null) {
            res.json({ success: false, message: 'Ensure username is provided' });
        } else {
            // If criteria is met, save user to database
            user.save(function(err) {
                console.log(user.username);
                if (err) {
                    res.json({ success: false, message: 'Username already exists!' });
                } else {
                    res.json({ success: true, message: 'user created!' }); // If all criteria met, save user
                } 
            });
        }
    });

1 个答案:

答案 0 :(得分:1)

我不认为这是创建和保存新用户的正确方法。您可以通过实例化用户模型并将对象作为参数传递给它来创建新用户。要了解我的意思,请参阅下面的解决方案。



var Question = require('../models/questions'); 
var User = require('../models/users'); 
var Answer = require('../models/answers');
var jwt = require('jsonwebtoken');
var secret = "markov";
// you don't define new User model here!
// var user = new User(); 
var question = new Question();
var answer = new Answer();

// Export routes to the main server.js file
module.exports = function(router) {
    /* ====================
    User Registration Route
    ==================== */
    router.post('/users', function(req, res) {

        if (req.body.username == null) {
            res.json({ success: false, message: 'Ensure username is provided' });
        } else {
            // If criteria is met, save user to database
            // create new user 
            var newUser = new User({ username: req.body.username });
            newUser.save(function(err) {
                console.log(user.username);
                if (err) {
                    res.json({ success: false, message: 'Username already exists!' });
                } else {
                    res.json({ success: true, message: 'user created!' }); // If all criteria met, save user
                } 
            });
        }
    });