@ Norman .. thank you for reply....I am trying hard to know my error.. My code goes link this:
''''
const { body, validationResult } = require("express-validator");
const Customer = require('../models/custdetails');
const async = require('async');
''''
Then I have some code related to sanitization .. then customer object is created
''''
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,
});
''''
here firm feature and cust_need are both arrays, then
''''
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);
}
]
''''
My data is not getting inserted into database. I have tried every method. Please help
I have also tried as below
''''
(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);
}
''''
Below is my 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);
''''
CodePudding user response:
If you are trying to insert I would switch to "customer.save()" method. Here is an example from my code:
customer.save((err) => {
if (err) { return next(err); }
// Respond to request indicating the user was created
res.status(201).json({ customer: customer }).end();
});
CodePudding user response:
Ultimately, I could save data without array fields getting added, using .save as suggested by Norman and then I $pushed array into the same document by using findOneAndUpdate. I was thinking I could write full data including array fields in one go into the document, but that did not happen...Is this the only answer if I am using node.js, express, mongoose and mongodb?..I dont know.