Difficulties to find() in the schema/model Mongoose with req.params.

Asked

Viewed 67 times

1

As the title says, I’m having trouble making a find() customized in Node model/schema, which links with Mongoose.

My file correspondencia.js (model) below:

var mongoose = require('mongoose');

var CorrespondenciaSchema = new mongoose.Schema({
    id_processo: String,
    prioridade: String,
    tipo: String,
    remetente: String,
    destinatario: String,
    data_prazo: Date,
    data_emissao: Date,
    data_conhecimento_envio: Date,  
    documento: String,
    tags: String,
    setor_responsavel_interno: String,
    pessoa_responsavel_interno: String,
    setor_responsavel_externo: String,
    pessoa_responsavel_externo: String,
    assunto: String,
    descricao: String,
    acao: String,   
    data_protocolo_interno: Date,
    data_protocolo_externo_sede: Date,
    data_protocolo_externo_regional: Date, 
    protocolo_interno: String,
    protocolo_externo_sede: String,
    protocolo_externo_regional: String,
    
    local_upload_documento: String,
    local_upload_anexos: String,
    local_upload_referencia_externa: String,
    id_circulacoes: String

});

/*
CorrespondenciaSchema.find({"id_processo": id_processo}, (err, correspondencias) => {
    if (err) {
        res.status(500).send(err)
    } else{
        res.status(200).send(correspondencias);
    }
});
*/
module.exports = mongoose.model('Correspondencia', CorrespondenciaSchema);

And the code snippet from the corresponding.js (router) file that will make the $get request:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Correspondencia = require('../models/correspondencia.js');
var path = require('path');
var multer = require('multer');

/* GET SINGLE CORRESPONDENCIA BY ID PROCESSO 
router.get('/processo/:id_processo', function(req, res, next) {
    Correspondencia.find(req.params.id_processo, function (err, post) {
        if (err) return next(err);
        res.json(post);
    });
});
*/


module.exports = router;

Remember that other gets, posts, puts and router tips that do not use other fields are working and returning the . json normally.

1 answer

0

After searching a little about, I managed to get, but he brings the "get all" and does not filter "Where" with the req.params.id_process

function findByIdProcesso(req, res){
var query  = CorrespondenciaSchema.where({ id_processo: req.params.id_processo });
query.find(function (err, correspondencias) {
    if (err)
        return res.send(err)
    res.json(correspondencias);
    });

};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.