如何正确集成Mongoose关系数据库
你能帮我看看这段代码,找出我哪里出错了吗?
我有我的猫鼬模式引用孩子。我想要的是创建一个用户并嵌入他的付款详细信息作为参考。我现在的结果允许我创建一个新用户,问题来自于当我想使用付款详细信息更新用户架构时。如果我在邮递员中发送请求,该请求将继续加载,直到我取消它。发生的情况是,如果我尝试获取 userInfo,我将看到 payment_Id 是架构中的引用,但我可以在父架构上调用 .populate 方法。下面是....
export const expoSignUp = async (req, res) => {
const { firstname, lastname, middlename, username, email, phoneNo , password } = req.body
try {
const userReg = await Expo.findOne({ username })
if (userReg) {
res.status(403).json({ message: "Username already taken"})
} else {
const encryptPass = CryptoJS.AES.encrypt(password, 'secret key 123').toString();
const userData = await Expo.create({
firstname, lastname, middlename, username, email, phoneNo, password: encryptPass
});
const result = jwt.sign({firstname, lastname, middlename, username, email, phoneNo},"secrete", {expiresIn:"24"})
res.status(200).json({ userData, result })
}
} catch (err) {
res.status(500).json({ message: err.message });
}
}
export const expoSignIn = async (req, res) => {
const { password, email, username } = req.body;
try {
const userLogin = await Expo.findOne({ username })
if (!userLogin) {
res.status(401).json({ message: "username is incorrect"})
} else {
const decryptPass = CryptoJS.AES.decrypt(userLogin.password, 'secret key 123');
var originalPass = decryptPass.toString(CryptoJS.enc.Utf8);
if (password !== originalPass) {
res.status(403).json({ message: "Login credentials is incorrect" })
} else {
const result = jwt.sign({ username, email, _id: userLogin._id }, "secrete", { expiresIn: "24" });
const {password, ...others} = userLogin._doc
res.status(200).json({ others, result })
}
}
} catch (error) {
res.status(500).json({message: error.message})
}
}
export const getAllExpoUsers = async (req, res) => {
try {
const getUsers = await Expo.find()
res.status(200).json(getUsers)
} catch (err) {
res.status(err)
}
}
export const paymentInfo = async (req, res) => {
const {
userId,
tx_ref,
amount,
currency,
payment_options,
customer: {
email,
phonenumber,
fullname,
},
customizations: {
title,
description,
logo
}
} = req.body;
try {
const userPaymentInfo = await Payment.create({
tx_ref,
amount,
currency,
payment_options,
customer: {
email,
phonenumber,
fullname,
},
customizations: {
title,
description,
logo
}
})
const newPaymentInfo = await Expo.findById({ _id: userPayment })
res.status(newPaymentInfo)
} catch (error) {
res.status(500).json({message:error.message})
}
}
can you help me look at this code to find out where I am making it wrong ?
I have my mongoose schema that am referencing child. What I want is to create a user and embed his payment details as a reference. my current result now, allows me to create anew user and the problems comes from when I want to update the user schema with payment details. If I send a request in postman, the request keep on loading until I cancel it. and what happend is if I try to get the userInfo, I will see that the payment_Id is reference in the schema but I can called .populate method on the parent schema. below is the....
export const expoSignUp = async (req, res) => {
const { firstname, lastname, middlename, username, email, phoneNo , password } = req.body
try {
const userReg = await Expo.findOne({ username })
if (userReg) {
res.status(403).json({ message: "Username already taken"})
} else {
const encryptPass = CryptoJS.AES.encrypt(password, 'secret key 123').toString();
const userData = await Expo.create({
firstname, lastname, middlename, username, email, phoneNo, password: encryptPass
});
const result = jwt.sign({firstname, lastname, middlename, username, email, phoneNo},"secrete", {expiresIn:"24"})
res.status(200).json({ userData, result })
}
} catch (err) {
res.status(500).json({ message: err.message });
}
}
export const expoSignIn = async (req, res) => {
const { password, email, username } = req.body;
try {
const userLogin = await Expo.findOne({ username })
if (!userLogin) {
res.status(401).json({ message: "username is incorrect"})
} else {
const decryptPass = CryptoJS.AES.decrypt(userLogin.password, 'secret key 123');
var originalPass = decryptPass.toString(CryptoJS.enc.Utf8);
if (password !== originalPass) {
res.status(403).json({ message: "Login credentials is incorrect" })
} else {
const result = jwt.sign({ username, email, _id: userLogin._id }, "secrete", { expiresIn: "24" });
const {password, ...others} = userLogin._doc
res.status(200).json({ others, result })
}
}
} catch (error) {
res.status(500).json({message: error.message})
}
}
export const getAllExpoUsers = async (req, res) => {
try {
const getUsers = await Expo.find()
res.status(200).json(getUsers)
} catch (err) {
res.status(err)
}
}
export const paymentInfo = async (req, res) => {
const {
userId,
tx_ref,
amount,
currency,
payment_options,
customer: {
email,
phonenumber,
fullname,
},
customizations: {
title,
description,
logo
}
} = req.body;
try {
const userPaymentInfo = await Payment.create({
tx_ref,
amount,
currency,
payment_options,
customer: {
email,
phonenumber,
fullname,
},
customizations: {
title,
description,
logo
}
})
const newPaymentInfo = await Expo.findById({ _id: userPayment })
res.status(newPaymentInfo)
} catch (error) {
res.status(500).json({message:error.message})
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论