gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:29 25 4
gpt4 key购买 nike

我正在尝试将一系列利率传递给由帐户组成的 Mongoose 模式。我想存储在特定日期变化的利率。

但是,当我触发创建函数时,我的开发工具告诉我我做了一些不好的事情:

**400 Bad Request**

我一直在使用this作为模板。

View :已断开连接,因此我只能通过:

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

执行更新的 Controller :

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

Mongoose 模式:

'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);

最佳答案

您可以尝试将利率保留为前端结构,而不是在模型中定义它,然后在帐户架构中将利息设置为 [] 类型,然后推送对象

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

});

就这样

关于angularjs - MEAN 堆栈应用程序中的错误请求尝试附加到架构中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com