Suppose in the listing(includes other attributes like title, price as well) i give a set of 20 members with their name and phone number.I need to automatically check whether these members already exists in the member list, if they exist then add return the price of the listing else add the member to the member list.
I am not able to define a particular schema for member model whether array or string or key value pair for member name and member phone number.if i define also i am not able to retrieve it from the database and compare it
So basically i want a member list like member 1 member2 … and when i create the listing these listing members should get compared with the members are return price if they exist else create a new member with that particular 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");
}