I'm very new to node and mongoose schemas, so this might be a noob question.
I'm trying to fetch the data to display on my home page. Right now, the home page uses a function in a separate file to call the Api and fetch from the backend, which seems to be working. On the backend of my route, I make two calls to separate DB functions. One that adds the payment to the payment collection and the other that updates the values in the user details collection.
When I make my db calls, it seems to get hung up and cannot complete the query. I tested this by just logging to the console and the log before the function prints, but the one after does not.
This is my router code:
const express = require('express');
const db = require('../db/DbCalls');
const historyRoutes = express.Router();
// will collect/sort by date all purchases, refunds, and payments for user
historyRoutes.route("/home").get( async function (req, res) {
let transactions = [];
const params = {
limit: 0
}
//retrieve lists
const purchases = await db.getPurchases(params);
const payments = await db.getPayments();
const refunds = await db.getRefunds();
//merge lists
transactions = purchases.concat(payments);
transactions = transactions.concat(refunds);
//sort lists by most recent transaction
transactions.sort(function(a,b){
return a.date - b.date;
});
res.json(transactions);
});
module.exports = historyRoutes;
These are my database call functions
const Purchase = require('../Schemas/purchase');
const Payment = require('../Schemas/payment');
const Refund = require('../Schemas/refund');
const User = require('../Schemas/user');
// All database functions used in router files
// retrieves all purchases
async function getPurchases(params) {
try {
console.log(params.limit);
console.log('entered');
Purchase.find().limit(params.limit).exec(
function (err, results ){
if (err) console.log('error');
console.log(results);
return results;
}
);
} catch (err) {
console.log('error');
throw (err);
}
}
// retrieves all payments
async function getPayments() {
try {
const results = await Payment.find().exec();
console.log(results);
return results;
} catch (err) {
console.log(err);
throw err;
}
}
// retrieves all refunds
async function getRefunds() {
try {
const results = await Refund.find().exec();
console.log(results);
return results;
} catch (err) {
console.log(err);
throw err;
}
}
// retrieves max limit, current limit, and current balance
async function getUserDetails() {
try{
const results = await User.findOne({name: 'user'}).exec();
console.log(results);
return results;
}catch (err) {
console.log(err);
throw err;
}
}
// updates user details
async function updateUserDetails(body) {
try {
const result = await User.updateOne({name:'user'},body).exec();
console.log(result);
return result;
} catch (err) {
console.log(err);
throw err;
}
}
// adds a single transaction
async function addPayment(body){
const payment = new Payment({
transType: 'payment',
amount:body.amount,
title: 'Paid to Fizz',
count: body.count,
date: Date.now()
});
thing.save().then(
()=> {
console.log('Added Payment Successfully');
return
}
).catch(
(err)=> {
console.log(`Error adding payment ${err}.`);
return
}
)
}
async function addTransaction(body) {
try {
let result;
if (body.type === 'purchase') {
result = await Purchase.insertOne(body);
}
if (body.type === 'payment') {
result = await Payment.insertOne(body);
}
if (body.type === 'refund') {
result = await Refund.insertOne(body);
}
console.log(result);
return result;
} catch (err) {
throw err;
}
}
module.exports = {addTransaction, updateUserDetails, getPayments, getPurchases, getRefunds, getUserDetails};
These are my schemas
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
const paymentSchema = new Schema({
transType: {type: String, required:true},
amount: {type: Number, required:true},
title: {type: String, required:true},
rfndStatus : {type: Boolean, required:true},
count : {type: Number, required:true},
date: {type: Date, required:true}
});
module.exports = mongoose.model("payment", paymentSchema);
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
const purchaseSchema = new Schema({
transType: {type: String,required:true},
amount: {type: Number,required:true},
title: {type: String,required:true},
rfndStatus : {type: Boolean,required:true},
date: {type: Date,required:true}
});
module.exports = mongoose.model('purchase', purchaseSchema);
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
const refundSchema = new Schema({
transType: {type: String, required:true},
title: {type: String, required:true},
amount: {type: Number, required:true},
date: {type: Date, required:true}
});
module.exports = mongoose.model("refund", refundSchema);
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
const userSchema = new Schema({
name: {type:String},
balance: {type: Number, required: true},
currentLimit: {type:Number, required: true},
MaximumLimit: {type: Number, required: true}
});
module.exports = mongoose.model("user", userSchema);
I don't really know where I'm going wrong in structure.