我正在使用 csvtojson npm 包将 csv 数据导入 mongodb,我收到 ValidationError: Contact validation failed: contactdateofbirth: Cast to date failed for value "Invalid Date" at path "contactdateofbirth", dayoftheyear: Cast to Number failed for value路径“dayoftheyear”处的“NaN”错误。
这是我到目前为止所做的:
[
{
contactsalutation: 'Mr',
contactname: 'Jone White',
contactaddress: '456 Some Road',
contactnumber1: '12345678',
contactnumber2: '87654321',
contactemail: 'demo@demo.com',
contactdateofbirth: '23/01/1970',
contactremark: 'he is a very patient customer'
},
{
contactsalutation: 'Ms',
contactname: 'Jane doly',
contactaddress: '123 Some Other Road',
contactnumber1: '81234567',
contactnumber2: '81234578',
contactemail: 'demo@hotmail.com',
contactdateofbirth: '28/02/1970',
contactremark: 'she is also a very patient customer'
}
]
csvtojson()
.fromFile(csvFilePath)
.then(jsonObj => {
console.log(jsonObj);
jsonObj.forEach(items => {
const importContact = new Contact({
contactsalutation: items.contactsalutation,
contactname: items.contactname,
contactaddress: items.contactaddress,
contactnumber1: items.contactnumber1,
contactnumber2: items.contactnumber2,
contactemail: items.contactemail,
contactdateofbirth:new Date(items.contactdateofbirth),
dayoftheyear: (moment(new Date(items.contactdateofbirth)).format('YYYY/MM/DD') == '1900/01/01') ? '0' : moment(new Date(items.contactdateofbirth)).dayOfYear(),
contactremark: items.contactremark,
});
importContact.save()
})
})
这是架构的简短版本:
contactdateofbirth:{
type: Date,
default: '1900-01-01'
},
dayoftheyear:{
type:Number,
default: '0'},
我尝试使用新日期,但未能转换为正确的日期。我在这里想念什么?非常感谢,非常感谢任何帮助。再次感谢。