【问题标题】:Insert multiple values using using angular strap select multiple and mongoose使用角带选择多个和猫鼬插入多个值
【发布时间】:2015-01-11 09:18:34
【问题描述】:

我正在尝试使用 mongoose db 和角带选择多个执行多个插入。问题是每次我插入位置时,我在 mongodb 中得到的都是“位置”:[ {“名称”:“[object Object],[object Object]”,“_id”:ObjectId(“54b1bbdabbc1a9c603c4c1e2”)}] 这个是我的架构

var userSchema = new mongoose.Schema({
username: { type: String, lowercase: true, unique: true },
email: { type: String, lowercase: true, unique: true },
profile: {
    name: String,
    gender: String,
    locations: [{
        name: String
    }],
    created_at: Date,
    updated_at: { type: Date, default: Date.now },
},
});

路线

app.post('/api/users/profile/:username', function(req, res, next) {
if (req.params.username) {
    User.findOne({ username: req.params.username }, function(err, user) {
        if (err) return next(err);
        user.profile.name = req.body.name || '';
        user.profile.gender = req.body.gender || '';
        user.profile.locations.push({ name: req.body.location.name });

        user.save(function(err) {
            if (err) return next(err);
            res.send(200);
        });
    });
}else{
    return res.send(400, { message: 'User does not exist!!' });
}
});

HTML

<button type="button" class="btn btn-lg btn-block btn-default" name="location.name" ng-       model="location.name"data-html="1" data-multiple="1" data-animation="am-flip-x" ng-options="country.name for country in countries" bs-select>
Action <span class="caret"></span></button>

角度

angular.module('Chat')
.controller('ProfileCtrl', ['$scope', '$routeParams', 'Profile', function($scope,     $routeParams, Profile) {
    $scope.profile = function() {
        Profile.updateProfile({
            username: $routeParams.username,
            name: $scope.name,
            gender: $scope.gender,
            locations: $scope.location.name
        })
    }

    $scope.countries = [{
        name: 'Nueva York'
    }, {
        name: 'Mozambique'
    }, {
        name: 'Tulua'
    }, {
        name: 'Australia'
    }];
}]);

【问题讨论】:

    标签: angularjs express mongoose angular-strap


    【解决方案1】:

    没关系.. 不需要做 user.profile.locations.push({ name: req.body.location.name });只是 user.profile.locations = req.body.location || [];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2020-02-16
      • 2016-10-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2014-09-04
      相关资源
      最近更新 更多