2
respostas

Node e mongo

controller.js

const extend = require("extend");

const logger = require("../../helpers/logger");

const mongoose = require("mongoose");

const zanzar = require("../../mongo");

const controllerActions = {};

const customMethods = {

  perquisar: async (req, res) => {

    logger.info("Dashboard :: ");

    await 

      zanzar

      .find()

      .then((result) => {

        res.status(200).send(result);

        logger.info("Dashboard :: result :: " + result.length);

      })

      .catch((error) => {

        logger.info({ error: error.message, stack: error.stack });

        res.status(400).json("Erro ao montar informações do gráfico !");

      });

  },

  listarCampanhas: async (req, res) => {

    logger.info("listarCampanhas :: ");

    await mongoose

      .model("dashboards")

      .find()

      .then((result) => {

        res.status(200).send(result);

        logger.info("listarCampanhas :: result :: " + result.length);

      })

      .catch((error) => {

        logger.info({ error: error.message, stack: error.stack });

        res.status(400).json("Erro buscar lista de campanhas !");

      });

  },

};

extend(controllerActions, customMethods);

module.exports = controllerActions;

mongo.js

const mongoose = require("mongoose");

const logger = require("./helpers/logger");

mongoose.Promise = global.Promise;

mongoose

  .connect("mongodb://127.0.0.1:27017/zanzar", { useNewUrlParser: true })

  .then(() => logger.info("mongodb conectado :: "))

  .catch((erro) => logger.error(erro));

const Zanzar = new mongoose.Schema({

  data_hora: {

    type: Date,

    required: true,

  },

  nome_campanha: {

    type: String,

    required: true,

  },

  idCampanha: {

    type: Number,

    required: true,

  },

  idEvento: {

    type: String,

    required: true,

  },

  idPlayer: {

    type: Number,

    required: true,

  },

  latitude: {

    type: Number,

    required: true,

  },

  longitude: {

    type: Number,

    required: true,

  },

  nomePlayer: {

    type: Number,

    required: true,

  },

});

module.exports = { Mongoose: mongoose, zanzar: Zanzar }
2 respostas

Está dando este erro:

(node:26052) UnhandledPromiseRejectionWarning: TypeError: zanzar.find is not a function
    at perquisar (C:\sistemas\zanzar\ApiDriverApi\api\v2\dashboard\controller.js:16:8)
    at Layer.handle [as handle_request] (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\layer.js:95:5)
    at C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:335:12)
    at next (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:174:3)
    at router (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:317:13)
    at C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:335:12)
    at next (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:174:3)
    at router (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:317:13)
    at C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:335:12)
    at next (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:275:10)
    at C:\sistemas\zanzar\ApiDriverApi\api\app.js:29:2
    at Layer.handle [as handle_request] (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:317:13)
    at C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:335:12)
    at next (C:\sistemas\zanzar\ApiDriverApi\node_modules\express\lib\router\index.js:275:10)
    at internalNext (C:\sistemas\zanzar\ApiDriverApi\node_modules\helmet\index.js:47:33)
    at xXssProtection (C:\sistemas\zanzar\ApiDriverApi\node_modules\x-xss-protection\index.js:26:7)
    at internalNext (C:\sistemas\zanzar\ApiDriverApi\node_modules\helmet\index.js:51:7)
    at nosniff (C:\sistemas\zanzar\ApiDriverApi\node_modules\dont-sniff-mimetype\index.js:4:5)
    at internalNext (C:\sistemas\zanzar\ApiDriverApi\node_modules\helmet\index.js:51:7)
    at ienoopen (C:\sistemas\zanzar\ApiDriverApi\node_modules\ienoopen\dist\index.js:5:9)

O que pode ser ?

Bom dia Guilherme, tudo certo?

Esse problema está relacionado à algum curso específico? Isso ajudaria a encontrar uma solução para o seu projeto.

A mensagem em si diz que a chamada zanzar.find() no arquivo controller.js não é uma função. Provavelmente zanzar deveria ser um Model, de acordo com a documentação do Mongoose: https://mongoosejs.com/docs/api.html#model_Model.find.

Espero ter ajudado, qualquer coisa estou no aguardo. Bons estudos!