@诺曼..谢谢你的回复....我正在努力知道我的错误..我的代码去链接这个:
''''
const { body, validationResult } = require("express-validator");
const Customer = require('../models/custdetails');
const async = require('async');
''''
然后我有一些与清理相关的代码..然后创建客户对象
''''
const customer = new Customer({
firm_name : req.body.firm_name,
firm_feature : req.body.firm_feature,
first_name: req.body.first_name,
last_name: req.body.last_name,
mob_no: req.body.mob_no,
cust_email : req.body.cust_email,
date_of_onboard: req.body.date_of_onboard,
date_of_visit: req.body.date_of_visit,
cust_need : req.body.cust_need,
status : req.body.status,
contact_back : req.body.contact_back,
});
''''
这里firm features和cust_need都是数组,那么
''''
const data = req.body;
customer.update({$push: {customer: data},function(err, res){if (err) {
console.log("This is the error while inserting data:", err);
}else {console.log(res); } } });
res.redirect(customer.url);
}
]
''''
我的数据无法插入数据库。我已经尝试了所有方法。请帮助
我也试过如下
''''
(async function(){
try {
const filter = { first_name: req.body.first_name};
const options = {upsert: true};
const result = await customer.updateOne(filter, {$set:{data}}, options).then(function(){
console.log("data is inserted");
console.log('${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)')
})
}catch(err) {
console.log("Some error has occurred in insertion");
console.log(err);
};
});
res.status(200).redirect(customer.url);
}
''''
下面是我的custdetails.js
''''
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CustSchema = new Schema(
{
firm_name:{type: String, maxLength:50},
firm_feature: {type : { type: String }, enum: ['Private Limited Company', 'Public Limited Company',
'Partnerships Company', 'Limited Liability Partnership LLP',
'One Person Company', 'Sole Proprietorship',
'Section 8 Company']},
first_name: {type: String, required: true, maxLength: 100},
last_name: {type: String, required: true, maxLength: 100},
mob_no: {type: Number, required: true, maxLength:10},
cust_email:{type: String, lowercase: true}, //always converts emailto lowercase before saving
date_of_onboard: {type: Date},
date_of_visit: {type: Date},
cust_need: {type : { type: String }, enum:['Four Wheeler Loan', 'Home Loan', 'Two Wheeler Loan']},
brperson: {type: Schema.Types.ObjectId, ref: 'Branch'},
status: {type: Schema.Types.ObjectId, ref: 'CustInstance'},
contact_back: {type: Schema.Types.ObjectId, ref: 'CustInstance' },
}
);
//Export model
module.exports = mongoose.model('Customer', CustSchema);
''''
1条答案
按热度按时间t3psigkw1#
如果你试图插入,我会切换到“customer.保存()”方法。下面是我的代码中的一个例子: