this is a very odd one that I don't quite understand why it's not working. I have 2 functions, one to add an item to a MongoDB collection. That is working fine. I have another function which is meant to search the collection for items matching my query. For some reason, the search function is returning 'not a function' and I'm wondering if anyone can help?
When I try and call findSimilarEvents()
I get TypeError: event.find is not a function
back in the console
My model:
var mongoose = require('mongoose');
const eventSchema = new mongoose.Schema({
eventTime: { type: Date, default: Date.now() },
eventLoggedFrom: String,
eventData: Object,
eventID: String,
ticketID: { type: String, default: "" },
ticketStatus: { type: String, default: "" },
eventLastUpdated: { type: Date, default: Date.now() },
})
module.exports = mongoose.model('event', eventSchema);
My working function:
exports.addEventToDB = async function(eventData, eventLoggedFrom, EventID) {
const event = new EventModel({
eventLoggedFrom: eventLoggedFrom,
eventData, // this is a JSON object
eventID: eventID
});
await event.save();
}
My non-working function:
exports.findSimilarEvents = async function(eventData, eventLoggedFrom) {
const event = new EventModel();
let similarEvents = await event.find({
eventData: eventData
});
for (var i = 0; i = similarEvents.length; i ) {
console.log("found similar event");
console.log(similarEvents[i]);
}
}
I'm happy to provide any other info if anyone has any thoughts!
Thank you in advance
CodePudding user response:
exports.findSimilarEvents = async function(eventData, eventLoggedFrom) {
let similarEvents = await EventModel.find({
eventData: eventData
});
for (var i = 0; i = similarEvents.length; i ) {
console.log("found similar event");
console.log(similarEvents[i]);
}
}
try this ...