Node.js 使用模型中的Mongoose.Schema.Types.Decimal在db.create上运行时出现ValidationError
版本:Node.js 使用模型中的Mongoose.Schema.Types.Decimal在db.create上运行时出现ValidationError,node.js,mongodb,mongoose-schema,Node.js,Mongodb,Mongoose Schema,版本: mongoose@4.11.5 mongodb服务器版本:3.4.7 型号: var mongoose = require('mongoose'), Schema = mongoose.Schema; var CheckSchema = new Schema({ providerName: String, number: String, date: { type: Date }, dateReceived: { type: Date }, am
mongoose@4.11.5 mongodb服务器版本:3.4.7 型号:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CheckSchema = new Schema({
providerName: String,
number: String,
date: { type: Date },
dateReceived: { type: Date },
amount: Schema.Types.Decimal,
paymentType: String,
comment: String,
status: String,
images: [Image],
transactions: [Transaction],
rejectedTransactions: [Transaction],
payerName: String
});
服务器代码:
Check.create(newCheck, function (err, check) {
if (err) {
return _response.handleError(req, res, err, _logType);
}
return _response.handleSuccess(req, res, 200, _logType, check);
});
返回此错误:
Check validation failed: amount: Cast to Decimal128 failed for value
"123.98" at path "amount"
ValidationError
“金额”以数字形式来自客户。当我将“amount”字符串化时,它成功地将其存储为十进制128类型
newCheck.amount = newCheck.amount.toString();
我不想每次创建新支票时都这样做。我有一个大型应用程序,我想把乘法值改为小数128。我的设置中是否缺少某些内容 您可以考虑使用
{type:Number}
表示金额