S
S
Sergey2015-02-03 20:06:10
MongoDB
Sergey, 2015-02-03 20:06:10

How to save in mongoose?

How to save two fields to a model

name: {
        ru: {
            type: String,
            default: '',
            required: 'Please fill field ru',
            trim: true
        },
        en:
                {
                    type: String,
                    default: '',
                    required: 'Please fill field en',
                    trim: true
                }
    },
?
In angular controller I do
// Create new Knowledge
    $scope.create = function() {
      // Create new Knowledge object
      var knowledge = new Knowledges ({
        
                                ru: this.name.ru,
                                en: this.name.en
      });

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

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

in view
<div class="form-group">
                    <label class="control-label" for="ru">Name ru</label>
                    <div class="controls">
                        <input type="text" data-ng-model="ru" id="ru" class="form-control" placeholder="Name ru" required>
                    </div>
                    <label class="control-label" for="en">Name en</label>
                    <div class="controls">
                        <input type="text" data-ng-model="en" id="en" class="form-control" placeholder="Name en" required>
                    </div>
                </div>

Writes
TypeError: Cannot read property 'ru' of undefined
UPDATED
Solved with
var knowledge = new Knowledges ({				
                                name: this.name                                
      });

and
<div class="controls">
                        <input type="text" data-ng-model="name.ru" id="ru" class="form-control" placeholder="Name ru" required>
                    </div>
                    <label class="control-label" for="en">Name en</label>
                    <div class="controls">
                        <input type="text" data-ng-model="name.en" id="en" class="form-control" placeholder="Name en" required>
                    </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2015-02-03
@mrRontgen

Try like this:

var knowledge = new Knowledges ({
        
                                ru: $scope.ru,
                                en: $scope.en
      });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question