Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not printing table data correctly in case of mongoose JSON array response #18

Closed
sp-suresh opened this issue Jul 24, 2018 · 3 comments
Closed

Comments

@sp-suresh
Copy link

I am trying to print mongoose json array using console.table, However I see unexpected table header. Below is my code:

const cTable = require('console.table');
const mongoose = require('mongoose');
mongoose.connect('mongodb:/host', {
	auth: {
		user: 'unm',
		password: 'pwd'
	}
});

const pairNm = 'BTC-INR-tx'



const pairModel = mongoose.model(pairNm, {}, pairNm);

pairModel.find({}, {_id: 0, trans_id: 1,
    fill_qty: 1,
    fill_price: 1,
    fill_flags: 1,
    currencyPair: 1,
    lastModifiedDate: 1}).limit(10).exec().then((d) => {
	// console.log(d);
	console.table('d', d);
});

Here is the log:

d
------------------------------------------------------
$__              isNew  errors  _doc             $init
---------------  -----  ------  ---------------  -----
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true 
[object Object]  false          [object Object]  true

Whereas actual data look like this:

[ { trans_id: 382,
    fill_qty: 10000000,
    fill_price: 712500,
    fill_flags: 3,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521712287144 },
  { trans_id: 384,
    fill_qty: 5000000,
    fill_price: 711800,
    fill_flags: 2,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521714892456 },
  {},
  { trans_id: 392,
    fill_qty: 10000000,
    fill_price: 711200,
    fill_flags: 1,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521787237696 },
  { trans_id: 393,
    fill_qty: 10000000,
    fill_price: 711200,
    fill_flags: 3,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521787298539 },
  { trans_id: 394,
    fill_qty: 21168,
    fill_price: 715000,
    fill_flags: 2,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521790941289 },
  { trans_id: 395,
    fill_qty: 978832,
    fill_price: 715000,
    fill_flags: 3,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521791070426 },
  { trans_id: 399,
    fill_qty: 100000000,
    fill_price: 716000,
    fill_flags: 3,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521798458247 },
  { trans_id: 400,
    fill_qty: 22510000,
    fill_price: 710000,
    fill_flags: 2,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521800843473 },
  { trans_id: 400,
    fill_qty: 77490000,
    fill_price: 709000,
    fill_flags: 1,
    currencyPair: 'BTC-INR',
    lastModifiedDate: 1521800843474 } ]
@bahmutov
Copy link
Owner

bahmutov commented Jul 24, 2018 via email

@sp-suresh
Copy link
Author

Well, It's resolved by converting it to plain js object by using mongoose's lean

Below is the modified code:

const cTable = require('console.table');
const mongoose = require('mongoose');
mongoose.connect('mongodb:/host', {
	auth: {
		user: 'unm',
		password: 'pwd'
	}
});

const pairNm = 'BTC-INR-tx'



const pairModel = mongoose.model(pairNm, {}, pairNm);

pairModel.find({}, {_id: 0, trans_id: 1,
    fill_qty: 1,
    fill_price: 1,
    fill_flags: 1,
    currencyPair: 1,
    lastModifiedDate: 1}).limit(10).lean().exec().then((d) => {
	// console.log(d);
	console.table('d', d);
});

Which results in beautiful tabled response:

d
---------------------------------------------------------------------------
trans_id  fill_qty   fill_price  fill_flags  currencyPair  lastModifiedDate
--------  ---------  ----------  ----------  ------------  ----------------
382       10000000   712500      3           BTC-INR       1521712287144   
384       5000000    711800      2           BTC-INR       1521714892456   
                                                                           
392       10000000   711200      1           BTC-INR       1521787237696   
393       10000000   711200      3           BTC-INR       1521787298539   
394       21168      715000      2           BTC-INR       1521790941289   
395       978832     715000      3           BTC-INR       1521791070426   
399       100000000  716000      3           BTC-INR       1521798458247   
400       22510000   710000      2           BTC-INR       1521800843473   
400       77490000   709000      1           BTC-INR       1521800843474  

@sp-suresh
Copy link
Author

Mongoose response is not a plain js object, It has to be converted first before converting it to table structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants