MEAN堆栈应用程序中的错误请求尝试附加到架构中的数组

时间:2014-12-05 23:26:23

标签: angularjs node.js mongodb mean-stack

我正在尝试将一系列利率传递给由帐户组成的mongoose模式。 我想存储在某些日期发生变化的利率。

然而,当我触发创建功能时,我的开发工具告诉我,我做了一件坏事:

**400 Bad Request**

我一直使用this作为模板。

视图:已断开连接,因此我只传递:

var interest = {
    rate: 1,
    date: Date.now()
};

执行更新的控制器:

// 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;
    });
};

猫鼬模式:

'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 is the interest to be 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);

1 个答案:

答案 0 :(得分:0)

您可以尝试将利率作为前端结构而不是在模型中定义,然后在帐户模式中设置兴趣作为[]的类型并且只需推送对象

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'
}

});

就像这样