Mongodb - 无法从UI插入值

时间:2015-07-08 13:34:46

标签: angularjs node.js mongodb mean-stack

我正在研究MEAN堆栈应用程序,我正在尝试将数据从页面插入Mongodb,我能够插入除" Blood group"之外的所有值。血型组未插入数据库,而是我看到版本信息" __ V = 0"。我已经定义了下面的架构。

var userSchema = new Schema({
name: String,
age: Number,
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
city: String,
blood: String,
mobile: Number });

我的来自以下

             <form>
                <div class="form-group"><br>
                    <label class="lbl">Name:</label>
                    <input type="text" name="name"  autocomplete="off" required="" data-defaultclass="number" data-fieldclass="numberimg" data-actualval="Mobile Number or Email" data-placeholderval="" class="form-control changeplaceholder email number" pattern=".+" placeholder="Name" ng-model="formData.newName"><br>
                    <label class="lbl">Age:</label>
                    <input type="text" name="age"  autocomplete="off" required="" data-defaultclass="number" data-fieldclass="numberimg" data-actualval="Mobile Number or Email" data-placeholderval="" class="form-control changeplaceholder email number" pattern=".+" placeholder="Age" ng-model="formData.age"><br>
                    <label class="lbl">Username:</label>
                    <input type="text" name="username"  autocomplete="off" required="" data-defaultclass="number" data-fieldclass="numberimg" data-actualval="Mobile Number or Email" data-placeholderval="" class="form-control changeplaceholder email number" pattern=".+" placeholder="Username" ng-model="formData.uname"><br>
                    <label class="lbl">Password:</label>
                    <input type="text" name="password"  autocomplete="off" required="" data-defaultclass="number" data-fieldclass="numberimg" data-actualval="Mobile Number or Email" data-placeholderval="" class="form-control changeplaceholder email number" pattern=".+" placeholder="Password" ng-model="formData.passwrd"><br>
                    <label class="lbl">City/Town:</label>
                    <input type="text" name="place" autocomplete="off" required="" data-defaultclass="number" data-fieldclass="numberimg" data-actualval="Mobile Number or Email" data-placeholderval="" class="form-control changeplaceholder email number" pattern=".+" placeholder="City/Town" ng-model="formData.city"><br>
                    <label class="lbl">Blood Group:</label>                        
                    <input type="text" class="form-control changeplaceholder" autocomplete="off" required="" placeholder="ex: B +ve" ng-model="formData.blood" pattern=".+"><br>{{formData.blood}}
                    <label class="lbl">Mobile:</label>
                    <input type="text" name="mobile" autocomplete="off" required="" data-defaultclass="number" data-fieldclass="numberimg" data-actualval="Mobile Number or Email" data-placeholderval="" class="form-control changeplaceholder email number" pattern=".+" placeholder="Mobile Number" ng-model="formData.mobile"><br>
                </div>
                <button type="submit" class="btn btn-primary btn-lg" ng-click="createUser()">Register</button>
            </form>

我的控制器如下。

$scope.createUser = function() {
    $http.post('/api/users', $scope.formData)
        .success(function(data) {
            $scope.formData = {};
            $scope.users = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};

这是我在routes.js文件中的代码

app.post('/api/users', function(req, res) {
    User.create({
        name : req.body.newName,
        age : req.body.age,
        username : req.body.uname,
        password : req.body.passwrd,
        city : req.body.city,
        blood : req.body.blood,
        mobile : req.body.mobile,
        done : false
    }, function(err, user) {
        if (err)
            res.send(err);
        User.find(function(err, users) {
            if (err)
                res.send(err)
            res.json(users);
        });
    });
});

0 个答案:

没有答案