'mongoose populate isn't responding after sending data using postman. And Second when ever am updating the reference schema it keeps on loading

Here is the controller that is sending the data and checking also the schema. My two issues is this;

  1. When I wanted to update the parent schema using the reference schema, the request will persist till I cancel it, and the data will get posted, but it will not return a success message plus the data. 2; When I fin my data, I get all users but if I concat the populate method on the parent schema, the request will also keep on loading without any answer been return.

    import Expo from "../Models/Expo.js" import jwt from "jsonwebtoken" import CryptoJS from "crypto-js" import Payment from "../Models/ExpoUserTickets.js"

    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 } = 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); console.log(originalPass) 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().populate('payment') 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.findByIdAndUpdate(userId, { $push: { userPaymentInfo: userPaymentInfo._id }, }, { new: true }) res.status(newPaymentInfo) } catch (error) { res.status(500).json({message:error.message}) } }

Below is my Schema Parent Schema:

const paymentSchema = new mongoose.Schema({
   tx_ref: String,
    amount: Number,
    currency: String,
    payment_options: String,
    customer: {
      email: String,
      phonenumber: Number,
      fullname: String,
    },
    customizations: {
      title: String,
      description: String,
      logo: String,
    },
}, {timestamps:true})


const Payment = mongoose.model("Payment", paymentSchema)

Refence Schema:

const paymentSchema = new mongoose.Schema({
   tx_ref: String,
    amount: Number,
    currency: String,
    payment_options: String,
    customer: {
      email: String,
      phonenumber: Number,
      fullname: String,
    },
    customizations: {
      title: String,
      description: String,
      logo: String,
    },
}, {timestamps:true})


const Payment = mongoose.model("Payment", paymentSchema)

All effort are really appreciate



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source