Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!