angularjs - Bad request in MEAN stack app trying to append to an array in a schema -
i trying pass array of interest rates mongoose schema consisting of accounts. want store interest rates change @ dates.
however, when trigger create function dev tools tell me have done bad:
**400 bad request**
i have been using this template.
the view: has been disconnected pass:
var interest = { rate: 1, date: date.now() };
the controller updating:
// create new account $scope.create = function() { // create new account object var account = new accounts ({ name: this.name, desc: this.desc, interests: [] }); // problematic part // store interest: var interest = { rate: 1, date: date.now() }; account.interests.push(interest); // problematic part end // redirect after save account.$save(function(response) { $location.path('accounts/' + response._id); // clear form fields $scope.name = ''; }, function(errorresponse) { $scope.error = errorresponse.data.message; }); };
the mongoose schema:
'use strict'; /** * module dependencies. */ var mongoose = require('mongoose'), schema = mongoose.schema; /** * * interest rate schema */ var interestrate = new schema({ rate:{ type: number, default: 0, trim: true }, date:{ type : date, default: '', required: 'when interest updated', trim: true } }); /** * account schema */ var accountschema = new schema({ name: { type: string, default: '', required: 'please fill account name', trim: true }, desc:{ type: string, default: '', trim: true }, interests: [{ type : schema.types.objectid, ref: 'interestrate' }], amount:{ type: number, default:0, trim: true }, created: { type: date, default: date.now }, user: { type: schema.objectid, ref: 'user' } }); mongoose.model('account', accountschema);
you can try leaving interest rate frond end structure , not define in model, in account schema set interests type of [] , push objects
var accountschema = new schema({ name: { type: string, default: '', required: 'please fill account name', trim: true }, desc:{ type: string, default: '', trim: true }, interests: type:[], default:[] }, created: { type: date, default: date.now }, user: { type: schema.objectid, ref: 'user' }
});
just this
Comments
Post a Comment