Using YQL in Node JS - javascript

enter code hereI am trying to extract content out of a website for learning purposes. I used YQL for that and it gave me JSON back(https://developer.yahoo.com/yql/). I thought I was making progress but unfortunately I was not able to get same output via NPM module. Following is my code:
var YQL = require('yql');
new YQL.exec('select * from html where url="http://www.natnlawcenter.com/United-States-Car-Dealerships/Alabama.aspx" ', function(response) {
console.log(response);
});
and following is my output:
{ query:
{ count: 1,
created: '2015-09-27T23:51:25Z',
lang: 'en-US',
results: { body: [Object] } } }
How do I access content of body:[Object]?
Thanks for your time.
I have modified the code as below:
request({
method: 'GET',
url: 'http://www.natlawcenter.com/United-States-Car-Dealerships/Alabama.aspx'
}, function(err, response, body) {
if (err) return console.error(err);
// Tell Cherrio to load the HTML
$ = cheerio.load(body);
console.log($('td').each(function(i, element){
var a = $(this);
console.log(a);
}));
});
and following is my output:
{ options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
_root:
{ '0':
{ type: 'root',
name: 'root',
attribs: {},
children: [Object],
next: null,
prev: null,
parent: null },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
length: 1,
_root: [Circular] },
length: 0,
prevObject:
{ options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
_root: { '0': [Object], options: [Object], length: 1, _root: [Circular] },
length: 0,
prevObject: { '0': [Object], options: [Object], length: 1, _root: [Circular] } } }
[Function]
[Function]
[Function]
[Function]
[Function]
{ '0':
{ type: 'tag',
name: 'td',
attribs: { valign: 'top', width: '999' },
children: [ [Object], [Object] ],
next:
{ data: '\r\n\t\t\t\t\t\t\t\t',
type: 'text',
next: null,
prev: [Circular],
parent: [Object] },
prev:
{ data: '\r\n\t\t\t',
type: 'text',
next: [Circular],
prev: null,
parent: [Object] },
parent:
{ type: 'tag',
name: 'tr',
attribs: {},
children: [Object],
next: [Object],
prev: [Object],
parent: [Object] } },
-------------------------------
'188':
{ type: 'tag',
name: 'td',
attribs: { width: '25%', icobalt: 'System.Web.UI.ITemplate' },
children:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
next:
{ type: 'tag',
name: 'td',
attribs: [Object],
children: [Object],
next: [Object],
prev: [Circular],
parent: [Object] },
prev:
{ type: 'tag',
name: 'tr',
attribs: [Object],
children: [Object],
next: [Circular],
prev: [Object],
parent: [Object] },
parent:
{ type: 'tag',
name: 'tbody',
attribs: {},
children: [Object],
next: null,
prev: null,
parent: [Object] } },
How can I access whats in children object of for example '188'?
Thanks for your time.

You need parse the JSON response in to JS object using JSON.parse(). Your code can we re-written like so -
request({
method: 'GET',
url: 'http://www.natlawcenter.com/United-States-Car-Dealerships/Alabama.aspx'
}, function(err, response, body) {
if (err) return console.error(err);
if (response.statusCode === 200 && body) return JSON.parse(body);
});

Related

MongoDB find() giving wrong object?

I am running a Mongo query from the Order database which aims to fetch the orders of a particular user by using his email. This is done using an API, but I am getting the complete object with some unnecessary details.
Code
I have written the following API in nextJS name myorders:
import Order from "../../models/Order";
import connectDB from "../../middleware/mongoose";
import jsonwebtoken from "jsonwebtoken";
const handler = async(req, res) => {
const token = req.body.token;
const data = jsonwebtoken.verify(token,process.env.JWT_SECRET);
console.log(data)
let mere_orders = Order.find({email: data.email})
console.log("mereorders12 = ", mere_orders)
res.status(200).json({mere_orders});
}
export default connectDB(handler);
And console.log("mereorders12 = ", mere_orders) gives me this:
mereorders12 = Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
_executionStack: null,
mongooseCollection: Collection {
collection: Collection { s: [Object] },
Promise: [Function: Promise],
modelName: 'Order',
_closed: false,
opts: {
autoIndex: true,
autoCreate: true,
schemaUserProvidedOptions: [Object],
capped: false,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'orders',
collectionName: 'orders',
conn: NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 1,
_closeCalled: undefined,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb://localhost:27017/chesswear',
_connectionOptions: [Object],
client: [MongoClient],
'$initialConnection': [Promise],
db: [Db],
host: 'localhost',
port: 27017,
name: 'chesswear'
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
model: Model { Order },
schema: Schema {
obj: {
email: [Object],
orderId: [Object],
paymentInfo: [Object],
products: [Object],
address: [Object],
subtotal: [Object],
status: [Object]
},
paths: {
email: [SchemaString],
orderId: [SchemaString],
paymentInfo: [SchemaString],
products: [Mixed],
address: [SchemaString],
subtotal: [SchemaNumber],
status: [SchemaString],
_id: [ObjectId],
updatedAt: [SchemaDate],
createdAt: [SchemaDate],
__v: [SchemaNumber]
},
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: { initializeTimestamps: [Function (anonymous)] },
methodOptions: {},
statics: {},
tree: {
email: [Object],
orderId: [Object],
paymentInfo: [Object],
products: [Object],
address: [Object],
subtotal: [Object],
status: [Object],
_id: [Object],
updatedAt: [Function: Date],
createdAt: [Object],
__v: [Function: Number],
id: [VirtualType]
},
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object] ],
'$id': 1,
mapPaths: [],
s: { hooks: [Kareem] },
_userProvidedOptions: { timestamps: true },
options: {
timestamps: true,
typeKey: 'type',
id: true,
_id: true,
validateBeforeSave: true,
read: null,
shardKey: null,
discriminatorKey: '__t',
autoIndex: null,
minimize: true,
optimisticConcurrency: false,
versionKey: '__v',
capped: false,
bufferCommands: true,
strictQuery: true,
strict: true,
pluralization: true
},
'$timestamps': { createdAt: 'createdAt', updatedAt: 'updatedAt' },
'$globalPluginsApplied': true,
_requiredpaths: [ 'status', 'subtotal', 'address', 'products', 'orderId', 'email' ]
},
op: 'find',
options: {},
_conditions: { email: 'mohit6#test.com' },
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: NodeCollection {
collection: Collection {
collection: [Collection],
Promise: [Function: Promise],
modelName: 'Order',
_closed: false,
opts: [Object],
name: 'orders',
collectionName: 'orders',
conn: [NativeConnection],
queue: [],
buffer: false,
emitter: [EventEmitter]
},
collectionName: 'orders'
},
_traceFunction: undefined,
'$useProjection': true
}
But I should return order like this:
{
orders: [
{
"_id":"62693ae3f7fd0b7d87c8eb9c"},
"email":"mohit3#test.com",
"orderId":"1389629752594",
"paymentInfo":"No payment info",
"products":{...},
"address":"adasdklfasflka",
"subtotal":4,
"status":"Paid",
"createdAt":{"$date":"2022-04-27T12:45:23.352Z"},
"updatedAt":{"$date":"2022-04-27T12:45:23.352Z"},"__v":0
},
{
"_id":"62693ae3f7fd0b7d87c8eb9c"},
"email":"mohit3#test.com",
"orderId":"1389629752594",
"paymentInfo":"No payment info",
"products":{...},
"address":"adasdklfasflka",
"subtotal":14,
"status":"Paid",
"createdAt":{"$date":"2022-04-27T12:45:23.352Z"},
"updatedAt":{"$date":"2022-04-27T12:45:23.352Z"},"__v":0
}
]
}
Additionally this is the model schema of Order
const mongoose = require("mongoose");
const OrderSchema = new mongoose.Schema(
{
email: { type: String, required: true },
orderId: { type: String, required: true },
paymentInfo: { type: String, default: "No payment info" },
products: { type: Object, required: true },
address: { type: String, required: true },
subtotal: { type: Number, required: true },
status: { type: String, required: true, default: "Pending" },
},
{ timestamps: true }
);
export default mongoose.models.Order || mongoose.model("Order", OrderSchema);
Please help.
From Model.find(), it returns Query object.
Following the example, you need to execute the query asynchronously.
let mere_orders = await Order.find({email: data.email}).exec();
Or
let mere_orders = await Order.find({email: data.email});
As you are trying to return JSON as:
{
orders: [...]
}
You should return the response as below:
res.status(200).json({ orders: mere_orders });

Cheerio - Get correct text when selector returns more than one result

https://www.cbf.com.br/futebol-brasileiro/competicoes/campeonato-brasileiro-serie-a/2018/1?ref=botao
I would like to get the market text from the page above. ("Sábado, 14 de Abril de 2018" and "16:00").
I did this with kotlin and the jsoup library:
val date = select("div.col-sm-8 > span.text-2")[1] //Sábado, 14 de Abril de 2018
val time = select("div.col-sm-8 > span.text-2")[2] //16:00
This query div.col-sm-8 > span.text-2 returns an array and I simple get the right information using the index.
But due to other issues, I have to use javascript.
I tried to do the same thing using JavaScript and the Cherio library but it seems that it doesn't work the same way, even if both search mode are based in JQuery:
const scherio = require('cheerio');
const rp = require('request-promise');
/**
* #type {string}
*/
const baseurl = "https://www.cbf.com.br/futebol-brasileiro/competicoes/campeonato-brasileiro-serie-a/2018/";
const turn = 190;
let totalGames = 1;
const gamesPerRound = 10;
module.exports =
class FetchRoundsFromCbf {
fetchRounds() {
for (let i = 1; i <= totalGames; i++) {
let url = baseurl.concat(i.toString());
rp(url).then(function (html) {
const $ = scherio.load(html);
let date = $("div.col-sm-8 > span.text-2")[1];
let time = $("div.col-sm-8 > span.text-2")[2];
console.log(date.text());
console.log(time.text());
});
}
}
}
Gives me:
Unhandled rejection TypeError: date.text is not a function
at /home/alexandre/dev/flutter/brasileiro-parser-js/network/fetchdata/FetchRoundsFromCbf.js:32:39
at tryCatcher (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/promise.js:694:18)
at _drainQueueStep (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/alexandre/dev/flutter/brasileiro-parser-js/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:637:19)
Then I print only the query result:
console.log(date);
console.log(time);
I receive:
{ type: 'tag',
name: 'span',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object: null prototype] { class: 'text-2 p-r-20' },
'x-attribsNamespace': [Object: null prototype] { class: undefined },
'x-attribsPrefix': [Object: null prototype] { class: undefined },
children:
[ { type: 'tag',
name: 'i',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [],
parent: [Circular],
prev: null,
next: [Object] },
{ type: 'text',
data: ' Sábado, 14 de Abril de 2018',
parent: [Circular],
prev: [Object],
next: null } ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object: null prototype] { class: 'col-sm-8' },
'x-attribsNamespace': [Object: null prototype] { class: undefined },
'x-attribsPrefix': [Object: null prototype] { class: undefined },
children:
[ [Object],
[Object],
[Object],
[Circular],
[Object],
[Object],
[Object] ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: null,
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: [Object] } },
prev:
{ type: 'text',
data: '\n ',
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'tag',
name: 'span',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Circular] },
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Object] },
prev: [Circular],
next:
{ type: 'tag',
name: 'span',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Circular],
next: [Object] } } }
{ type: 'tag',
name: 'span',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object: null prototype] { class: 'text-2 p-r-20' },
'x-attribsNamespace': [Object: null prototype] { class: undefined },
'x-attribsPrefix': [Object: null prototype] { class: undefined },
children:
[ { type: 'tag',
name: 'i',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [],
parent: [Circular],
prev: null,
next: [Object] },
{ type: 'text',
data: ' 16:00',
parent: [Circular],
prev: [Object],
next: null } ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object: null prototype] { class: 'col-sm-8' },
'x-attribsNamespace': [Object: null prototype] { class: undefined },
'x-attribsPrefix': [Object: null prototype] { class: undefined },
children:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Circular],
[Object] ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: null,
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: [Object] } },
prev:
{ type: 'text',
data: '\n ',
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'tag',
name: 'span',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Circular] },
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: [Object],
next: [Object] },
prev: [Circular],
next: null } }
I'm not very good at javascript, how would I do to retrieve the information I need?
You can use eq() to get a Cheerio element by index, the same way as in jQuery.
let date = $("div.col-sm-8 > span.text-2").eq(1);
let time = $("div.col-sm-8 > span.text-2").eq(2);
eq() reduces the set of matched elements to the one at the specified index.
I managed what I want by using slice:
let date = $("div.col-sm-8").find("span").slice(1);
let time = $("div.col-sm-8").find("span").slice(2);
console.log(date.text());
console.log(time.text());

How to display the results of MongoDB $near query

So I am running a legacy $near search here
SoundSpot.find(
{ location : { $near : [ longitude, latitude ], $maxDistance: 100 } }
);
But I'm confused on what is actually happening to what I'm finding. To my understanding and research the search will show the documents sorted by location nearest to the given coordinates, but anything that i do to try to see the documents only outputs this large Query. i have no idea what this giant Query means, im not vary familiar with mongo and javascript and i am trying to figure out how exactly i see what the $near did.
Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts:
{ bufferCommands: true,
capped: false,
'$wasForceClosed': undefined },
name: 'soundspots',
collectionName: 'soundspots',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: null,
pass: null,
name: 'user_account',
options: null,
otherDbs: [],
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
_connectionOptions: [Object],
'$initialConnection': [Object],
db: [Object],
client: [Object] },
queue: [],
buffer: false,
emitter:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined } },
model:
{ [Function: model]
hooks: Kareem { _pres: [Object], _posts: [Object] },
base:
Mongoose {
connections: [Object],
models: [Object],
modelSchemas: [Object],
options: [Object],
_pluralize: [Function: pluralize],
plugins: [Object] },
modelName: 'soundspot',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: null,
pass: null,
name: 'user_account',
options: null,
otherDbs: [],
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
_connectionOptions: [Object],
'$initialConnection': [Object],
db: [Object],
client: [Object] },
discriminators: undefined,
'$appliedMethods': true,
authenticate: [Function],
serializeUser: [Function],
deserializeUser: [Function],
register: [Function],
findByUsername: [Function],
createStrategy: [Function],
'$appliedHooks': true,
schema:
Schema {
obj: [Object],
paths: [Object],
aliases: {},
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: [Object],
inherits: {},
callQueue: [],
_indexes: [],
methods: [Object],
statics: [Object],
tree: [Object],
query: {},
childSchemas: [],
plugins: [Object],
s: [Object],
_userProvidedOptions: undefined,
options: [Object],
'$globalPluginsApplied': true },
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'soundspots',
collectionName: 'soundspots',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
'$init': Promise { [Object], catch: [Function] } },
schema:
Schema {
obj:
{ name: [Function: String],
key: [Function: String],
connected: [Object],
type: [Object],
location: [Object],
playlist: [Object] },
paths:
{ name: [Object],
key: [Object],
'connected.username': [Object],
type: [Object],
'location.type': [Object],
'location.coordinates': [Object],
'location.longitude': [Object],
'location.latitude': [Object],
'playlist.title': [Object],
'playlist.file': [Object],
'playlist.votes': [Object],
_id: [Object],
username: [Object],
hash: [Object],
salt: [Object],
__v: [Object] },
aliases: {},
subpaths: {},
virtuals: { id: [Object] },
singleNestedPaths: {},
nested: { connected: true, location: true, playlist: true },
inherits: {},
callQueue: [],
_indexes: [],
methods:
{ setPassword: [Function],
changePassword: [Function],
authenticate: [Function] },
statics:
{ authenticate: [Function],
serializeUser: [Function],
deserializeUser: [Function],
register: [Function],
findByUsername: [Function],
createStrategy: [Function] },
tree:
{ name: [Function: String],
key: [Function: String],
connected: [Object],
type: [Object],
location: [Object],
playlist: [Object],
_id: [Object],
username: [Object],
hash: [Object],
salt: [Object],
__v: [Function: Number],
id: [Object] },
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
s: { hooks: [Object] },
_userProvidedOptions: undefined,
options:
{ typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
'$globalPluginsApplied': true },
op: 'find',
options: {},
_conditions:
{ location: { '$near': [Object], '$maxDistance': 100 },
_mongooseOption: 'find' },
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
NodeCollection {
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'soundspots',
collectionName: 'soundspots',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
collectionName: 'soundspots' },
_traceFunction: undefined }
Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts:
{ bufferCommands: true,
capped: false,
'$wasForceClosed': undefined },
name: 'soundspots',
collectionName: 'soundspots',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: null,
pass: null,
name: 'user_account',
options: null,
otherDbs: [],
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
_connectionOptions: [Object],
'$initialConnection': [Object],
db: [Object],
client: [Object] },
queue: [],
buffer: false,
emitter:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined } },
model:
{ [Function: model]
hooks: Kareem { _pres: [Object], _posts: [Object] },
base:
Mongoose {
connections: [Object],
models: [Object],
modelSchemas: [Object],
options: [Object],
_pluralize: [Function: pluralize],
plugins: [Object] },
modelName: 'soundspot',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: null,
pass: null,
name: 'user_account',
options: null,
otherDbs: [],
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
_connectionOptions: [Object],
'$initialConnection': [Object],
db: [Object],
client: [Object] },
discriminators: undefined,
'$appliedMethods': true,
authenticate: [Function],
serializeUser: [Function],
deserializeUser: [Function],
register: [Function],
findByUsername: [Function],
createStrategy: [Function],
'$appliedHooks': true,
schema:
Schema {
obj: [Object],
paths: [Object],
aliases: {},
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: [Object],
inherits: {},
callQueue: [],
_indexes: [],
methods: [Object],
statics: [Object],
tree: [Object],
query: {},
childSchemas: [],
plugins: [Object],
s: [Object],
_userProvidedOptions: undefined,
options: [Object],
'$globalPluginsApplied': true },
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'soundspots',
collectionName: 'soundspots',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
'$init': Promise { [Object], catch: [Function] } },
schema:
Schema {
obj:
{ name: [Function: String],
key: [Function: String],
connected: [Object],
type: [Object],
location: [Object],
playlist: [Object] },
paths:
{ name: [Object],
key: [Object],
'connected.username': [Object],
type: [Object],
'location.type': [Object],
'location.coordinates': [Object],
'location.longitude': [Object],
'location.latitude': [Object],
'playlist.title': [Object],
'playlist.file': [Object],
'playlist.votes': [Object],
_id: [Object],
username: [Object],
hash: [Object],
salt: [Object],
__v: [Object] },
aliases: {},
subpaths: {},
virtuals: { id: [Object] },
singleNestedPaths: {},
nested: { connected: true, location: true, playlist: true },
inherits: {},
callQueue: [],
_indexes: [],
methods:
{ setPassword: [Function],
changePassword: [Function],
authenticate: [Function] },
statics:
{ authenticate: [Function],
serializeUser: [Function],
deserializeUser: [Function],
register: [Function],
findByUsername: [Function],
createStrategy: [Function] },
tree:
{ name: [Function: String],
key: [Function: String],
connected: [Object],
type: [Object],
location: [Object],
playlist: [Object],
_id: [Object],
username: [Object],
hash: [Object],
salt: [Object],
__v: [Function: Number],
id: [Object] },
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
s: { hooks: [Object] },
_userProvidedOptions: undefined,
options:
{ typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
'$globalPluginsApplied': true },
op: 'find',
options: {},
_conditions:
{ location: { '$near': [Object], '$maxDistance': 100 },
_mongooseOption: 'find' },
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
NodeCollection {
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'soundspots',
collectionName: 'soundspots',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
collectionName: 'soundspots' },
_traceFunction: undefined }
Whenever you need to apply $geometry queries then don't forget to apply index in your schema like this:
location: {
type: [Number], // <Longitude, Latitude>
index: {
type: '2dsphere',
sparse: false
},
required: true,
},
Without index(2dsphere) you can't use $geoNear and $near in your aggregatation.
For ex:
db.places.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
//coordinates: [longitude, latitude]
distanceField: "dist.calculated",
maxDistance: 200, //Meters
includeLocs: "dist.location",
num: 5,
spherical: true
}
}
]);
In above code near find the nearest place from coordinates you requested.
distanceField find the distance in meters and display like this:
"dist" : {
"calculated" : 42107.6268114667, //Meters
"location" : [
-74.167457,
40.3650877
]
}
maxDistance allows you to find location in range of specified distance in meteres.
num is like $limit you can restrict how many data will return.
This is because you're printing the value of the Query object, instead of iterating on the result.
You are using Mongoose, but the issue is the same if you're using the native node driver.
For example, if I have one document in my test collection:
> db.test.find()
{ "_id": 0, "a": 1, "b": 1, "c": 1, "d": 1 }
Now if I run this following code, it will have a similar output to what you saw:
MongoClient.connect('mongodb://localhost:27017/test', (err, db) => {
db.db('test').collection('test').find({}, function(err, res) {
console.log(res);
});
});
Note that in the code above, I'm printing the res object. The output of the code is:
Cursor {
pool: null,
server: null,
disconnectHandler:
Store {
s: { storedOps: [], storeOptions: [Object], topology: [Server] },
length: [Getter] },
bson: BSON {},
ns: 'test.test',
cmd:
{ find: 'test.test',
limit: 0,
skip: 0,
query: {},
readPreference: ReadPreference { mode: 'primary', tags: undefined, options: undefined },
slaveOk: true },
options:
{ readPreference: ReadPreference { mode: 'primary', tags: undefined, options: undefined },
skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
db:
.... many more lines ....
The difference is that instead of a Query object, I see the Cursor object.
Now if I iterate on the res:
MongoClient.connect('mongodb://localhost:27017/test', (err, db) => {
db.db('test').collection('test').find({}, function(err, res) {
res.forEach(doc => {console.log(doc)});
});
});
It will print the actual document:
{ _id: 0, a: 1, b: 1, c: 1, d: 1 }
Check out Mongoose's page on the Query object for examples in Mongoose.

html manipulation with Node JS

I want to get html from a source (link or file , ...) and
find values from it.
html format is :
<!doctype html>
<html>
<body>
<main>
<section id="serp">
<div>
<article>a</article>
<article>b</article>
<article>c</article>
<article>d</article>
</div>
</section>
</main>
</body>
</html>
first of all i used cheerio.
according to docs i write:
const cheerio = require('cheerio');
const $ = cheerio.load(myhtml);
const content = $('#serp div').children();
console.log(content); // null
According to the same procedure i used x-ray and jsdom but all of them
print null.
I've done the following:
let myhtml = `<!doctype html>
<html>
<body>
<main>
<section id="serp">
<div>
<article>a</article>
<article>b</article>
<article>c</article>
<article>d</article>
</div>
</section>
</main>
</body>
</html>`;
const cheerio = require('cheerio');
const $ = cheerio.load(myhtml);
const content = $('#serp div').children();
console.log(content);
console.log(`html: ${content.html()}`);
it output the following to the console:
initialize {
'0':
{ type: 'tag',
name: 'article',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [ [Object] ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Object],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: null,
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: [Object] } },
'1':
{ type: 'tag',
name: 'article',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [ [Object] ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Object],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Object],
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: [Object] } },
'2':
{ type: 'tag',
name: 'article',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [ [Object] ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Object],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Object],
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: [Object] } },
'3':
{ type: 'tag',
name: 'article',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [ [Object] ],
parent:
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Object],
parent: [Object],
prev: [Object],
next: [Object] },
prev:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Object],
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: null } },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
_root:
initialize {
'0':
{ type: 'root',
name: 'root',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Object],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] },
length: 4,
prevObject:
initialize {
'0':
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Object],
parent: [Object],
prev: [Object],
next: [Object] },
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
_root: initialize { '0': [Object], options: [Object], length: 1, _root: [Circular] },
length: 1,
prevObject: initialize { '0': [Object], options: [Object], length: 1, _root: [Circular] } } }
html: a
Process finished with exit code 0

NodeJS, Mongoose, return variable

thanks in advance for your time and answers:
I've been trying to do something that "should" be easy but it's being me crazy.
the objective is to asign the result of a function to a variable that i can use later on a POST to send information to my MongoDB.
I've a model with a document as:
{ "__v" : 0, "_id" : ObjectId("54ad1aa637ce5c566c13d18f"), "num" : 9 }
What i want is to capture this number "num" : 9 to a variable. I created a function that query the mongodb through the model.
function getNum(){
var Num = require('./models/num.js');
var callback = function(){
return function(err, data) {
if(err) {
console.log("error found: " + err);
}
console.log("the number is: " + data.num);
}
};
return Num.findOne({}, callback());
};
then just to test i assign that function to a variable and try to console.log it just to test if the result is fine.
// =====================================
// TESTING ==============================
// =====================================
app.get('/testing',function(req, res, next){
var a = getNum();
console.log(a);
});
My output is:
{ _mongooseOptions: {},
mongooseCollection:
{ collection:
{ db: [Object],
collectionName: 'num',
internalHint: null,
opts: {},
slaveOk: false,
serializeFunctions: false,
raw: false,
pkFactory: [Object],
serverCapabilities: undefined },
opts: { bufferCommands: true, capped: false },
name: 'num',
conn:
{ base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'project',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_events: {},
db: [Object] },
queue: [],
buffer: false },
model:
{ [Function: model]
base:
{ connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
modelName: 'Num',
model: [Function: model],
db:
{ base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'project',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_events: {},
db: [Object] },
discriminators: undefined,
schema:
{ paths: [Object],
subpaths: {},
virtuals: [Object],
nested: {},
inherits: {},
callQueue: [Object],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
options: [Object],
_events: {} },
options: undefined,
collection:
{ collection: [Object],
opts: [Object],
name: 'num',
conn: [Object],
queue: [],
buffer: false } },
op: 'findOne',
options: {},
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
{ collection:
{ collection: [Object],
opts: [Object],
name: 'num',
conn: [Object],
queue: [],
buffer: false } },
_castError: null }
**the number is: 9**
This is one of the results i can get in other aproaches i get undefined.
Can anyone wonder what can i do to solve this?
Again thanks for your help.
You can't return the result of an asynchronous function like findOne, you need to use callbacks.
So it would need to be rewritten as something like:
function getNum(callback){
var Num = require('./models/num.js');
return Num.findOne({}, callback);
};
app.get('/testing',function(req, res, next){
getNum(function(err, a) {
console.log(a);
});
});

Categories