H
H
hollanditkzn2017-12-26 10:17:57
Node.js
hollanditkzn, 2017-12-26 10:17:57

How to fetch data and display everything via ajax?

I'm trying to make a request via ajax, but for some reason I don't see a specific output in the console.
In the router

const Router = require('koa-router'),
    Order = require('../models/order'),
    router = new Router();
router.get('/order/:id', async(ctx) => {
        let order = Order.find({orderId: ctx.params.id}).exec((err, order) => {
            if (err) return err;
            return order;
        });
        console.log(ctx.params.id);
    })

In ajax request
let key = lineTr.data('key');
            console.log(key);
            $.get({
                url: '/order/'+key,
            }).done((order) => console.log(order));

But in response I get this
_____________
Mongoose: users.findOne({ _id: ObjectId("5a152fefe8af8011c03ddbbc") }, { fields: {} })
3
Mongoose: orders.find({ orderId: 3 }, { fields: {} } )
--> GET /order/3 404 29ms -
_____________
In the browser console
_____________
3
jquery.min.js:4 GET localhost:3000/order/1 404 (Not Found)
_____________
But I need all the data that should be output from the schema

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hollanditkzn, 2017-12-26
@hollanditkzn

Issue resolved, should have done this

router.get('/order/:id', async(ctx) => {
        await Order.findOne({orderId: ctx.params.id}).exec((err, id) => {
            if (err) return err;
            ctx.body = id;
        });
    })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question