I am getting the error while trying to save following data.
Following is the error:
if (fields[key].$elemMatch) {
^
TypeError: Cannot read property '$elemMatch' of undefined at model.Query._castFields (C:\Users\hp\Documents\node-projects\musicfest1\node_modules\mongoose\lib\query.js:4306:23) at model.Query. (C:\Users\hp\Documents\node-projects\musicfest1\node_modules\mongoose\lib\query.js:1971:23) at model.Query._wrappedThunk [as _findOne] (C:\Users\hp\Documents\node-projects\musicfest1\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8) at process.nextTick (C:\Users\hp\Documents\node-projects\musicfest1\node_modules\kareem\index.js:369:33) at process.internalTickCallback (internal/process/next_tick.js:70:11)*
var data = {
eventid: 'eventezy01',
eventname: event_name,
title1: title1,
title2: title2,
dateVenue: dateVenue,
date: date,
venue: venue,
hsTicket: hsTicket,
ticketLink: ticketLink,
ticketPrice: ticketPrice,
about_heading: about_heading,
aboutEvent1: aboutEvent1,
singer_heading: singer_heading,
hsSinger: hsSinger,
sponsor_heading: sponsor_heading,
hsSponsor: hsSponsor,
singer: arrSinger,
sponsors: arrSponsor,
flocation: flocation,
femail: femail,
fcontact: fcontact,
fbLink: fbLink,
twitterLink: twitterLink,
youTubeLink: youTubeLink,
gPlusLink: gPlusLink,
instaLink: instaLink,
color: '#f50136',
bgImage: 'slider.png',
font: '"Montserrat", sans-serif'
};
FestData.findOne({eventid: 'eventezy01'}, data, options, (err, doc) => {
if(err) {
console.log(err);
}
if(!doc) {
var festData = new FestData(data);
festData.save();
}
This happens because you're passing in data to the second parameter of the
findOne
method, but that argument is for selecting which fields to include. If the found record doesn't have any one of those fields, it will produce this error. You should supplynull
as the second argument tofindOne
instead.See the docs on findOne