Imagine a scenario where I provide a list of 20 members with their names and phone numbers in a listing that also includes other attributes like title and price. The goal is to automatically check if these members already exist in the member list. If they do, return the price of the listing; if not, add the member to the member list. I am facing challenges in defining a specific schema for the member model—whether it should be an array, string, or key-value pair for the member's name and phone number. Even if I define a schema, retrieving it from the database for comparison purposes proves difficult. Essentially, what I need is a member list format such as member1, member2, etc. When creating the listing, these members should be compared with existing members to determine if they should return the price or create a new member entry with the provided name and phone number.
Listing Model
const listingSchema = new Schema({
title: { type: String },
price: { type: Number },
startDate: {
type: Date,
},
currentMonth: { type: Number },
endDate: {
type: Date,
},
member: { type: String },
phone: { type: Number },
});
Member Model
const memberSchema = new Schema({
member: "String",
phone: "Number",
});
Listing Controller
const newListing = new listing({
title: req.body.title,
price: req.body.price,
startDate: req.body.startDate,
currentMonth: req.body.currentMonth,
endDate: req.body.endDate,
member: req.body.member,
});
let member = newListing.member;
// console.log(member);
let present = await members.findOne([member]);
console.log(present);
member.forEach(async (e) => {
// console.log(e);
let existingMember = await members.findOne({ name: e });
if (existingMember) {
console.log("yes");
console.log(req.body.price)
} else {
console.log("no");
//createmember;
}
});
Member Controller
try {
const newMember = new member({
member: [{ name: req.body.name, phone: req.body.phone }],
});
console.log(newMember);
// let savedMember = await newMember.save();
// console.log(savedMember);
return res.json({ message: "New Members Created!" });
} catch (error) {
console.log(error);
res.status(404).json(error, "cannot create new member");
}