Node.js 如何通过nodejs从一个集合传递一个id,并使用它来使用mongodb本机驱动程序设置另一个模型
所以我有两条邮政路线。一个是创建新用户,另一个是使用同一用户创建新帖子。创建一个新用户将返回一个userID。我的问题是如何使用该用户ID,以便在同一用户创建新的post请求后将其引用到db.collection(“listings”)中Node.js 如何通过nodejs从一个集合传递一个id,并使用它来使用mongodb本机驱动程序设置另一个模型,node.js,mongodb,express,Node.js,Mongodb,Express,所以我有两条邮政路线。一个是创建新用户,另一个是使用同一用户创建新帖子。创建一个新用户将返回一个userID。我的问题是如何使用该用户ID,以便在同一用户创建新的post请求后将其引用到db.collection(“listings”)中 app.post("/messanger/createListing", (req, res) => { var productID = new ObjectID(); db.collection("listings&qu
app.post("/messanger/createListing", (req, res) => {
var productID = new ObjectID();
db.collection("listings")
.insertOne({
title: req.body.title,
description: req.body.description,
price: req.body.price,
type: req.body.type,
productID: productID,
})
.then(() => {
res.json({
status: "success",
message: "Listing has been successfully created.",
data: [
{
title: req.body.title,
description: req.body.description,
price: req.body.price,
type: req.body.type,
productID: productID,
},
],
});
})
.catch(() => {
res.send("Failed to create listings");
});
app.post("/messanger/createUser", (req, res) => {
const { username, password } = req.body;
const userID = new ObjectID();
db.collection("users")
.insertOne({
username,
password,
userID: userID,
})
.then((user) => {
return res.json({
status: "success",
message: `${username} has been created. Please login! `,
user: {
username,
userID: userID,
},
});
});
}))