我遇到以下代码的两个问题
在放入API调用后保存文档时,它会抛出一条消息'找不到匹配的文件,用于id \“59c6607a1608fe26e8c7f574 \”“'
如果每个字段的必需属性值设置为true,则mongoose会抛出一条验证错误消息,指出未找到路径。例如,如果ii set addressLine1:{type:String,required:true}则抛出验证消息contact.addressLine1:路径contact.addressLine1
是必需的。但是,如果required属性设置为false,则不会抛出验证错误。
有人可以帮我纠正下面的代码 -
模型 -
var localTransportSchema = new Schema({
providerID: {
type: Number,
required: true,
trim: true,
unique: false
},
name: {
type: String,
required: true,
trim: false
},
contact: {
addressLine1: {
type: String,
required: false
},
addressLine2: {
type: String,
required: false
},
city: {
type: String,
required: false
},
postalCode: {
type: Number,
required: false
},
primaryContactNumber: {
type: Number,
required: false
},
secondaryContactNumber: {
type: Number,
required: false
},
serviceActiveFlag: {
type: String,
required: false,
enum: ['Y', 'N']
},
},
vehicle: [{
vehicleType: {
type: String,
required: false,
enum: ['sedan', 'hatchback', 'suv', 'mpv', 'luxury']
},
activeFlag: {
type: String,
required: false,
enum: ['Y', 'N']
},
}]
});
控制器 -
module.exports.updateLocalTransportVendorDtls = function(req, res) {
var transportSchema = new transportModel();
new Promise(function(resolve, reject) {
checkForNewVehicleType(req, function(doc) {
resolve(doc)
})
})
.then(function(doc) {
var updateJustServiceDtls = doc.split(/\|/)[2];
return addJustNewVehicleDetailsOnly(req, res)
}).then(function() {
transportSchema.save(function(error) {
if (error) {
logger.error("Error while updating record in transport details collection: - " + error.message)
return res.status(500).json({
"Message": error.message.trim()
});
}
})
}).catch(function(err) {
return res.json({
"Message": err.message
});
});
}
功能 -
var addJustNewVehicleDetailsOnly = function(req, res) {
var promise = new Promise(function(resolve, reject) {
var updates = {
$set: {
"contact": {
"addressLine2": req.body['addressLine2'],
"secondaryContactNumber": req.body['secondaryContactNumber'],
"serviceActiveFlag": req.body['serviceActiveFlag']
},
"$push": {
"vehicle": {
"vehicleType": req.body['vehicleType'],
"chargePerKiloMeter": req.body['chargePerKM'],
"selfDriven": req.body['isSelfDriven'],
"additionalCharges": req.body['additionalCharges'],
"driverBata": req.body['driverBata'],
"activeFlag": req.body['vehicleActiveFlag']
}
}
}
}
transportModel.findOneAndUpdate({
"name": req.body['providerName'],
"contact.postalCode": parseInt(req.body['postalCode'])
},
updates, {
returnOriginal: false,
upsert: false
},
function(err, doc) {
if (err) {
logger.error("Error while updating record : - " + err.message);
return reject(res.status(409).json({
"Message": "Error while updating transport details for provider " + req.body['providerName'] + " in transport details table"
}));
} else if (doc === null) {
logger.error("Error while updating record in transport details : - unable to update database");
return reject(res.status(409).json({
"Message": "Error while updating transport details for provider " + req.body['providerName'] + " due to " + err.message
}));
}
return resolve();
});
})
return promise;
}