サブドキュメント 'social' である 'twitter' キーに値を追加しようとしています: this.social.twitterコントローラー。それを使用するとフォームの送信が中断され、コンソールにエラーはありません。それをコメントアウトしてフォームを送信すると、送信され、空の文字列で mongodb に twitter と facebook のソーシャルが追加されます。
フォームが送信されると、名前と画像の値が追加されます。
私のスキーマ:
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>