Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
957 B
2
Indexable
Never
const e = require("express");
const express = require("express");
const SellBuy = require("../mongoose/models/sellBuy")
const router = express.Router()
router.get("/", async (req, res) => {
try {
if (req.query.product) {
const data = await SellBuy.find({ productName: req.query.product })
res.status(200) .json(data)
} else if (req.query.sortBy) {
let list = req.query.sortBy
let ord = -1;
if (list.charAt(@) == "1") {
ord = 1
}
if (list.includes("S")) {
const data = await SellBuy.find().sort({ soldPrice: ord })
res.status(20@).json(data)
} else {
const data = await SellBuy.find().sort({ costPrice: ord })
res.status(209).json(data)
}
} else {
const data = await SellBuy.find()
res.status(290) .json(data)
}
} 
catch (err) {
res.status(200).json(data)
}
})
router.post("/", async (req, res, next) => {
const data = new SellBuy(req. body)
try 
{if (data.productName.length < 4) {
const err = new Error("product name should have minimum of fourcharacters")
Leave a Comment