Hi im working on my first app with mongodb. Putting together the api and Ive been able to post and delete from my collection but for some reason I cant get anything to return from .find() I just get back
{
“_events”: {},
“_eventsCount”: 0
}
posting multiple files sorry in advance.
index.js
import app from "./server";
import { MongoClient } from "mongodb";
import BeautyDAO from "./dao/beautyDAO"
const port = process.env.PORT || 8181;
MongoClient.connect(
process.env.MDECK_DB_URI,
{
maxPoolSize: 100,
useNewUrlParser: true,
writeConcern: {wtimeout:3000}
},
)
.catch( err => {
console.error(err.stack);
process.exit(1);
})
.then(async client => {
await BeautyDAO.injectDB(client);
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
})
server.js
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import morgan from "morgan";
import beautyData from "./api/beautyData.route"
const app = express();
app.use(cors());
process.env.NODE_ENV !== 'prod' && app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// register API routes
app.use("/api/v1", beautyData)
// app.use("/post", beautyData)
// app.use("/status", express.static("build"))
// app.use("/", express.static("build"))
app.use("*", (req, res) => res.status(404).json({ error: "not found" }))
export default app;
beautyDAO.js
import { ObjectId } from "bson";
export let beauty
let MetaDeck
// const DEFAULT_SEARCH = [["ratings.viewr.numReviews", -1]]
export default class BeautyDAO {
static async injectDB(conn) {
if(beauty) return;
try{
MetaDeck = await conn.db("MetaDeck");
beauty = await conn.db("MetaDeck").collection("beauty");
console.log("connection to db established")
} catch (e) {
console.error(`unable to establish a connection handle in beautyDAO: ${e}`)
}
}
static async getSome() {
const query = {product: "calm shoes" }
let cursor
try {
cursor = await beauty.find(query)
} catch (e) {
console.error(`Unable to issue find command, ${e}`)
return { results: []}
}
}
static async postSome() {
try {
await beauty.insertOne({product: "Dyshco Dress"})
//return cursor
} catch (e) {
console.error(`Unable to issue find command, ${e}`)
return { results: []}
}
}
static async deleteSomething() {
try {
await beauty.deleteOne({product: "Fang Dress"})
} catch (error) {
}
}
}
beautyData.controller.js
import {BeautyDAO, beauty} from "../dao/beautyDAO";
export default class BeautyController {
static async apiGetAllData(req, res, next) {
//res.json({message: "hi im working on it"})
let ans = beauty.find({});
try {
res.json(ans)
} catch (err) {
res.status(500).json({error: err});
}
}
static async postSomething(req, res, next) {
await BeautyDAO.postSome();
res.json({message: "still working on it"})
// try {
// let response = {
// product: product
// }
// res.json(response)
// } catch (err) {
// res.status(500).json({error: err});
// }
}
static async apiDeleteSomething(req, res, next){
let ans = await beauty.deleteOne({product: "Fang Dress"})
try{
res.json(ans)
} catch (e){
res.json({message: "still working on it"})
}
}
}
beautyData.routes.js
import { Router } from "express";
import BeautyCtrl from "./beautyData.controller";
const router = new Router();
// router.route("/").get(BeautyCtrl.apiGetAllData);
// router.route("/search").get(BeautyCtrl.apiSearchData);
// router.route("/product-type").get(BeautyCtrl.apiGetProdTypeData);
// router.route("/id/:id").get(BeautyCtrl.apiGetProdById);
router.route("/").get(BeautyCtrl.apiGetAllData)
router.route("/addItem").post(BeautyCtrl.postSomething)
router.route("/delete").delete(BeautyCtrl.apiDeleteSomething)
export default router;