使用Angularjs和Mongoose创建子文档并保存值(meanjs)

时间:2014-09-14 22:51:59

标签: angularjs mongodb mongoose mean meanjs

我正在尝试在'twitter'键中添加一个值,该键是一个子文档'social',控制器中包含: this.social.twitter 。使用该中断表单提交,控制台中没有错误。如果我发表评论并提交表格,它会提交并添加社交与推特和脸书给mongodb空字符串。

表单提交时,会添加name和image的值。

我的架构

var McSchema = new Schema({
name: {
    type: String,
    default: '',
    required: 'A name is required',
    trim: true
},
image: {
    type: String,
    default: '',
    required: 'An image is required',
    trim: true
},
social: {
    twitter: {
        type: String,
        default: '',
        trim: true
    },
    facebook: {
        type: String,
        default: '',
        trim: true
    }
},
created: {
    type: Date,
    default: Date.now
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
}

});

我的控制器:

angular.module('mcs').controller('McsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Mcs',
function($scope, $stateParams, $location, Authentication, Mcs) {
    $scope.authentication = Authentication;

    // Create new MC
    $scope.create = function() {
        // Create new Mc object
        var mc = new Mcs ({
            name: this.name,
            image: this.image,
            twitter: this.social.twitter
        });

        // Redirect after save
        mc.$save(function(response) {
            $location.path('mcs/' + response._id);

            // Clear form fields
            $scope.name = '';
            $scope.image = '';
            $scope.twitter = '';
        }, function(errorResponse) {
            $scope.error = errorResponse.data.message;
        });
    };

    ...

]);

我的表单:

<section data-ng-controller="McsController">
<div class="page-header">
    <h1>New Mc</h1>
</div>
<div class="col-md-12">
    <form class="form-horizontal" data-ng-submit="create()" novalidate>
        <fieldset>
            <div class="form-group">
                <label class="control-label" for="name">Name</label>
                <div class="controls">
                    <input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="image">Image</label>
                <div class="controls">
                    <input type="text" data-ng-model="image" id="image" class="form-control" placeholder="image" required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label" for="social-twitter">Twitter</label>
                <div class="controls">
                    <input type="text" data-ng-model="mc.twitter" id="social-twitter" class="form-control" placeholder="@">
                </div>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-default">
            </div>
            <div data-ng-show="error" class="text-danger">
                <strong data-ng-bind="error"></strong>
            </div>
        </fieldset>
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

您在html中设置了data-ng-model="mc.twitter"但在javascript中尝试访问this.social.twitter。如果我没有错过任何东西,那就是你的问题。您应该更改其中任何一个以使它们相同,例如将您的html更改为:data-ng-model="social.twitter"