Difficulties in doing find () in schema / model mongoose with req.params.field in $ get from http

1

As the title says, I'm having a hard time making a find() customized in the model / schema of the node, which links to the mongoose.

My following correspondence.js file (model):

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 of the correspondence.js (router) file that will make the request $ get:

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 deletes of the router that do not use other fields are working and returning the .json normally.

    
asked by anonymous 08.11.2017 / 13:43

1 answer

0

After searching a bit, I was able to get it, but it does "get all" and does not do the "where" filter 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);
    });

};

    
08.11.2017 / 15:10