如何正确集成Mongoose关系数据库

发布于 2025-01-09 12:04:07 字数 2823 浏览 0 评论 0原文

你能帮我看看这段代码,找出我哪里出错了吗?

我有我的猫鼬模式引用孩子。我想要的是创建一个用户并嵌入他的付款详细信息作为参考。我现在的结果允许我创建一个新用户,问题来自于当我想使用付款详细信息更新用户架构时。如果我在邮递员中发送请求,该请求将继续加载,直到我取消它。发生的情况是,如果我尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文