Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Having control flow issue with mongoose model's find


I'm completely new to node and it's frameworks Koa and express. I've a mongoose model called Drawing and a router module for that.

Problem is with express routers I was able the get the data from database using Drawing.find method but with Koa, control is not even going into Drawing.find. And I'm not able to get the data at all. Please find the following related code and help me understand the things better.

This is my router module

import * as Router from "koa-router";
import Drawing from "../../models/drawing";

function getRoutesForDrawing(): Router {
    console.log("Inside getRoutes for drawing");
    let route = new Router();
    route.get("/drawing", function(context,next) {
        console.log("Inside /drawing");
        Drawing.find(function(err,drawings) {
            console.log("Not gettig executed");
            context.body = "Welcome";
        });
        //context.body = "Welcome";       
    });
}
export default getRoutesForDrawing();

And the model is

import mongoose = require("mongoose");

export interface IDrawing extends mongoose.Document {
  drawingId:Number,
  drawingName:String,
  updatedOn:Date,
  updatedBy:Number
};

export const DrawingSchema = new mongoose.Schema({
  drawingId:Number,
  drawingName:String,
  updatedOn:Date,
  updatedBy:Number
});

const Drawing = mongoose.model<IDrawing>('Drawing', DrawingSchema);
export default Drawing;

As you can see in my router module, the control is actually coming for /drawing and it's printing in console "Inside /drawing" but then control isn't coming to Drawing.find. I'm getting difficulty in understanding this.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's a little bit hard to figure out what's going on because it looks like you have problems all over the place. Let me point out the things that stand out:

  1. getRoutesForDrawing is declared to return a router and doesn't return anything
  2. Koa routes are not like express. In particular they are not callback based. They take either generator functions (Koa 1.x) or async functions (Koa 2.x). You seem to expect that it's wanting a callback function which won't work. Assuming koa 2.x, its router.get('/drawing', async(context) => {...});
  3. Assuming koa 2.x, you need to await the result of the mongoose methods, e.g. context.body = await Drawing.find({})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda