[英]How to insert data of a single form into two collections using nodejs and mongodb
how do i insert the first three data coming from req.body into event collection and rest into personal collection我如何将来自 req.body 的前三个数据插入事件集合,将 rest 插入个人集合
app.post("/api/bookings", async(req, res) => {
const {
eventName,
date,
venue,
firstname,
lastname,
email,
contact,
address1,
address2,
city,
state,
zip,
} = req.body;
try {
await new Event(req.body).save();
await new Personal(req.body).save()
console.log("inserted");
} catch (error) {
console.log(error);
}
})
You can create two different objects for event and personal to store data您可以为事件和个人创建两个不同的对象来存储数据
app.post("/api/bookings", async(req, res) => {
const {
eventName,
date,
venue,
firstname,
lastname,
email,
contact,
address1,
address2,
city,
state,
zip,
} = req.body;
const EventData = {
eventName,
data,
venue
}
const PersonalData = {
firstname,
lastname,
email,
contact,
address1,
address2,
city,
state,
zip,
}
try {
await new Event(EventData).save();
await new Personal(PersonalData).save()
console.log("inserted");
} catch (error) {
console.log(error);
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.