D
D
Devero972020-12-13 18:25:04
Mongoose
Devero97, 2020-12-13 18:25:04

How to find the necessary news by three models?

I pass data to the backend (normal url string) and process it this way:

const Tag = require('../models/tag.model')
const Card = require('../models/card.model')
const Post = require('../models/post.model')

module.exports.getData = async (req, res) => {
    try {
        const tag = await Tag.findOne({tag: req.params.data})
        const card = await Card.findOne({card: req.params.data})
        const post = await Post.findOne({post: req.params.data})
        if(tag) {
            res.json(tag)
        } else if(card) {
            res.json(card)
        } else if(post) {
            res.json(post)
        }
    } catch(e) {
        res.status(500).json(e)
    }
}
code>
После обработки одна из моделей находит у себя статью/новость и передают найденную новость обратно. Все работает, но может есть более красивый способ обработать?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2020-12-13
@yarkov

Make the next request only when the previous one did not find anything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question