首页 > 解决方案 > Node/Express Route 'Show' presenting too many results

问题描述

This is for the backend of a MEAN site.

I am currently trying to create several search functions based off of different params in the same mongoose model. For the Controller I have

show:(req,res) => {
        Activity.findById
        ({_id: req.params._id}).then(activity => {
            res.json(activity)
        })
        .then(console.log(req.query._id))
    },

    show: async (req, res, next) => {

        const linkClicked = await Activity.find
        ({linkClicked: req.params.linkClicked})
        .then(result => {
            res.send(result)

        })
        .then(next())
        .catch(err => (console.log(err)))
    },
    show:(req,res, next) => {
        Activity.find({caseAssigned: req.params.caseAssigned}).then(action => {
            res.json(action)
            next()
        })
        .catch(err => (console.log(err)))
    },
    show:(req,res, next) => {
        Activity.find({addedToQue: req.params.addedToQue}).then(activity => {
            res.json(activity)
            next()
        })
        .catch(err =>(console.log(err)))
    },
    show:(req, res, next) => {
        Activity.find({statusChanged: req.params.statusChanged}).then(activity => {
            res.json(activity)
            next()
        })
        .catch(err => (console.log(err)))
    },
show:(req,res) => {
        Activity.findById
        ({_id: req.params._id}).then(activity => {
            res.json(activity)
        })
        .then(console.log(req.query._id))
    },

    show: async (req, res, next) => {

        const linkClicked = await Activity.find
        ({linkClicked: req.params.linkClicked})
        .then(result => {
            res.send(result)

        })
        .then(next())
        .catch(err => (console.log(err)))
    },
    show:(req,res, next) => {
        Activity.find({caseAssigned: req.params.caseAssigned}).then(action => {
            res.json(action)
            next()
        })
        .catch(err => (console.log(err)))
    },
    show:(req,res, next) => {
        Activity.find({addedToQue: req.params.addedToQue}).then(activity => {
            res.json(activity)
            next()
        })
        .catch(err =>(console.log(err)))
    },
    show:(req, res, next) => {
        Activity.find({statusChanged: req.params.statusChanged}).then(activity => {
            res.json(activity)
            next()
        })
        .catch(err => (console.log(err)))
    },

For the routes I have:

const express = require('express');
const controllerRouter = express.Router();
const activityController = require("../controllers/activity")

controllerRouter.get("/", activityController.index)
// controllerRouter.get("/search/:_id", activityController.show)

controllerRouter.get("/event/:linkClicked/linkClicked/", activityController.show)
controllerRouter.get("/event/caseAssigned/:caseAssigned", activityController.show)
controllerRouter.get("/event/addedToQue/:addedToQue", activityController.show)
controllerRouter.get("/event/statusChanged/:statusChanged", activityController.show)

When I search in postman for /event/true/linkClicked I receive JSON that does not have linkClicked:true.

Solutions I have tried: 1) I tried implementing the next() function in my controller and adding async capability. I'm not sure if I implemented this correctly. I've made sure that my JSON data is accurate.

Any help is appreciated. (Also, this is my first post so if I have left out a necessary detail, please, let me know.)

标签: node.jsmongodbexpressmongoose

解决方案


推荐阅读