Thursday, 27 April 2017

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.



via Kishore Kumar Korada

No comments:

Post a Comment