Related
Running a nodejs code like
const snapshot = await db.collection('matches').where('series', '==', 'Series-2').get();
snapshot.forEach((doc) => {
console.log(doc.data());
});
Its returning me data like
{
inns: [
{
balls: [Array],
ballers: [Array],
bats: [Array],
isDeclared: false
},
{
bats: [Array],
balls: [Array],
isDeclared: false,
ballers: [Array]
},
{
bats: [Array],
ballers: [Array],
balls: [Array],
isDeclared: false
},
{
bats: [Array],
ballers: [Array],
isDeclared: false,
balls: [Array]
}
],
id: 'jgvjbvwF',
series: 'Series-2',
}
How can I get the full data i.e. instead of balls: [Array], it should be
balls: [
{ id: 1, runs: 0 },
{ id: 2, runs 1 },
...
]
You're getting full array of docs. So you want to extract all balls array from it.
This is example code for understand.
Suppose, if we're taking doc1 as our example.
So code will be :
let doc1 = snapshot[0].data().inns;
doc1.forEach((in)=> {
console.log(in.balls);
});
According to above code you can retrieve the all balls array from innings array
I need to assign the genre field to a new array but I am not sure how to only get that field, or how to call that field
var SpotifyWebApi = require('spotify-web-api-node');
var spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken('-----');
spotifyApi.searchArtists('artist:queen')
.then(function(data) {
console.log('Search tracks by "queen" in the artist name', data.body.artists.items);
}, function(err) {
console.log('Something went wrong!', err);
});
This is the terminal when calling it.
I only want the first response.
PS C:\Users\g\Documents\js> node spotifyTest
Search tracks by "queen" in the artist name [ { external_urls:
{ spotify: 'https://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d' },
followers: { href: null, total: 19579534 },
genres: [ 'glam rock', 'rock' ],
href: 'https://api.spotify.com/v1/artists/1dfeR4HaWDbWqFHLkxsg1d',
id: '1dfeR4HaWDbWqFHLkxsg1d',
images: [ [Object], [Object], [Object], [Object] ],
name: 'Queen',
popularity: 90,
type: 'artist',
uri: 'spotify:artist:1dfeR4HaWDbWqFHLkxsg1d' },
{ external_urls:
{ spotify: 'https://open.spotify.com/artist/3nViOFa3kZW8OMSNOzwr98' },
followers: { href: null, total: 1087117 },
genres: [ 'deep pop r&b', 'pop', 'r&b' ],
href: 'https://api.spotify.com/v1/artists/3nViOFa3kZW8OMSNOzwr98',
id: '3nViOFa3kZW8OMSNOzwr98',
images: [ [Object], [Object], [Object] ],
name: 'Queen Naija',
popularity: 68,
type: 'artist',
uri: 'spotify:artist:3nViOFa3kZW8OMSNOzwr98' } ]
You can access a field in a JSON object using the dot notation. Here's an example of replacing the genres of the first response with a new array.
let responseItems = [
{ external_urls: { spotify: 'https://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d' },
followers: { href: null, total: 19579534 },
genres: [ 'glam rock', 'rock' ],
href: 'https://api.spotify.com/v1/artists/1dfeR4HaWDbWqFHLkxsg1d',
id: '1dfeR4HaWDbWqFHLkxsg1d',
images: [ [Object], [Object], [Object], [Object] ],
name: 'Queen',
popularity: 90,
type: 'artist',
uri: 'spotify:artist:1dfeR4HaWDbWqFHLkxsg1d'
},
{
external_urls: { spotify: 'https://open.spotify.com/artist/3nViOFa3kZW8OMSNOzwr98' },
followers: { href: null, total: 1087117 },
genres: [ 'deep pop r&b', 'pop', 'r&b' ],
href: 'https://api.spotify.com/v1/artists/3nViOFa3kZW8OMSNOzwr98',
id: '3nViOFa3kZW8OMSNOzwr98',
images: [ [Object], [Object], [Object] ],
name: 'Queen Naija',
popularity: 68,
type: 'artist',
uri: 'spotify:artist:3nViOFa3kZW8OMSNOzwr98'
}
];
let firstResponse = responseItems[0];
console.log(JSON.stringify(firstResponse.genres, null, 2));
let newGenres = [ 'rock', 'jazz' ];
firstResponse.genres = newGenres;
console.log(JSON.stringify(firstResponse.genres, null, 2));
This should show the following in the console:
[
"glam rock",
"rock"
]
[
"rock",
"jazz"
]
I believe this one is a bug.
I am trying to write a simple web scraper with request and cheerio.
How I tried to solve it:
Yes, I played with other ways to define a selector.
Yes, I have investigated other stackoverflow questions.
Yes, I have created an issue on cheerio github, here is the link: https://github.com/cheeriojs/cheerio/issues/1252
Yes, I am a professional web developer and this is not the first time I do node.js
Update:
After some people pointed out, the issue was that needed dom nodes were created after my page was parsed and traversed by cheerio.
So the part of the page I requested simply was not there.
Any Ideas how to bypass that?
I use versions:
{
"name": "discont",
"version": "1.0.0",
"description": "Find when the item is on sale",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"express": "^4.16.4"
},
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"ejs": "^2.6.1",
"request": "^2.88.0"
}
}
This is the HTML I am trying to scrape:
The link is here:
https://www.asos.com/new-look-wide-fit/new-look-wide-fit-court-shoe/prd/10675413?clr=oatmeal&SearchQuery=&cid=6461&gridcolumn=1&gridrow=9&gridsize=4&pge=1&pgesize=72&totalstyles=826
This is my code:
request(url, options, function(error, response, html) {
if (!error) {
var $ = cheerio.load(html, { withDomLvl1: false });
// console.log("product-price", $("div.product-price")[0].attribs);
console.log("product-price", $("div#product-price > div"));
}
});
The console.log returns an empty array(unable to find nested div).
This is what I get in return:
initialize {
options:
{ withDomLvl1: false,
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: [Array],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: false,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] },
length: 0,
prevObject:
initialize {
'0':
{ type: 'root',
name: 'root',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Array],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: false,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] } }
but if I change my code to
request(url, options, function(error, response, html) {
if (!error) {
var $ = cheerio.load(html, { withDomLvl1: false });
// console.log("product-price", $("div.product-price")[0].attribs);
console.log("product-price", $("div#product-price"));
}
});
I get an array with a single element:
initialize {
'0':
{ type: 'tag',
name: 'div',
namespace: 'http://www.w3.org/1999/xhtml',
attribs:
{ class: 'product-price',
id: 'product-price',
'data-bind': 'component: { name: "product-price", params: {state: state, showGermanVatMessage: false }}' },
'x-attribsNamespace': { class: undefined, id: undefined, 'data-bind': undefined },
'x-attribsPrefix': { class: undefined, id: undefined, 'data-bind': undefined },
children: [],
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: [Object],
next: [Circular] },
next:
{ type: 'text',
data: '\n ',
parent: [Object],
prev: [Circular],
next: [Object] } },
options:
{ withDomLvl1: false,
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: [Array],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: false,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] },
length: 1,
prevObject:
initialize {
'0':
{ type: 'root',
name: 'root',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: {},
'x-attribsNamespace': {},
'x-attribsPrefix': {},
children: [Array],
parent: null,
prev: null,
next: null },
options:
{ withDomLvl1: false,
normalizeWhitespace: false,
xml: false,
decodeEntities: true },
length: 1,
_root: [Circular] } }
yet, I am not able to see children of the element (the children array is empty), and I am not able to perform any methods on the object such as find() or text()
Any help is welcome!
Cheerio only has access to the DOM before any special things like XHRs have happened. You would need puppeteer or nightmarejs for the post-js-rendered DOM
I have integrated Neo4j in my NodeJS (backend) and Angular6 (front-end) application. My query runs and i get the data of nodes and connected node network in my Neo4j Browser. However, i want that entire data of nodes being displayed through a particular query on my console (i.e. in NodeJS -> back-end and angular ->front-end). This is not happening . I get only the first node data. Please help me retrieve the entire displayed nodal data in NodeJS .
NodeJS code
neo4j-controller.js
// Require Neo4j
var neo4j = require('neo4j-driver').v1;
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var express = require('express');
var router = express.Router();
var app = express();
const driver = new neo4j.driver("bolt://localhost:11001", neo4j.auth.basic("neo4j", "ib1"));
const cypher = 'MATCH (n) RETURN count(n) as count';
app.set('views', path.join(__dirname, 'views'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
var session = driver.session();
var request = require('request');
router.post('/', seekParameter);
module.exports = router;
//working code below
// ------------------------------- Original Code ----------------------------------------
function seekParameter(req, res) {
console.log("INSIDE NODE JS CONTROLLER OF seekParameter");
console.log("BODY IS ", req.body);
session
.run(`MATCH p=()-[r:Parameter]->() RETURN p`)
.then(function (result){
result.records.forEach(function(record){
console.log("record = ", record);
console.log("result = ", result)
console.log("1] record._fields[0].properties=",record._fields[0].properties);
res.send(record);
});
})
.catch(function(err){
console.log("inside catch = " + err);
})
session.close();
}
output
INSIDE NODE JS CONTROLLER OF seekParameter
BODY IS undefined
record = Record {
keys: [ 'p' ],
length: 1,
_fields:
[ Path { start: [Object], end: [Object], segments: [Array], length: 1 } ],
_fieldLookup: { p: 0 } }
result = { records:
[ Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] } ],
summary:
ResultSummary {
statement:
{ text: ' MATCH p=()-[r:Parameter]->() RETURN p ;',
parameters: {} },
statementType: 'r',
counters: StatementStatistics { _stats: [Object] },
updateStatistics: StatementStatistics { _stats: [Object] },
plan: false,
profile: false,
notifications: [],
server: ServerInfo { address: 'localhost:11001', version: 'Neo4j/3.4.7' },
resultConsumedAfter: Integer { low: 9, high: 0 },
resultAvailableAfter: Integer { low: 1, high: 0 } } }
1] record._fields[0].properties= { name: 'accidentTime' }
2]record._fields[1] = [ Path {
start: Node { identity: [Object], labels: [Array], properties: [Object] },
end: Node { identity: [Object], labels: [Array], properties: [Object] },
segments: [ [Object] ],
length: 1 } ]
record = Record {
keys: [ 'p' ],
length: 1,
_fields:
[ Path { start: [Object], end: [Object], segments: [Array], length: 1 } ],
_fieldLookup: { p: 0 } }
result = { records:
[ Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] },
Record {
keys: [Array],
length: 1,
_fields: [Array],
_fieldLookup: [Object] } ],
summary:
ResultSummary {
statement:
{ text: ' MATCH p=()-[r:Parameter]->() RETURN p ;',
parameters: {} },
statementType: 'r',
counters: StatementStatistics { _stats: [Object] },
updateStatistics: StatementStatistics { _stats: [Object] },
plan: false,
profile: false,
notifications: [],
server: ServerInfo { address: 'localhost:11001', version: 'Neo4j/3.4.7' },
resultConsumedAfter: Integer { low: 9, high: 0 },
resultAvailableAfter: Integer { low: 1, high: 0 } } }
1] record._fields[0].properties= { name: 'productType' }
2]record._fields[1] = [ Path {
start: Node { identity: [Object], labels: [Array], properties: [Object] },
end: Node { identity: [Object], labels: [Array], properties: [Object] },
segments: [ [Object] ],
length: 1 } ]
inside catch = Error: Can't set headers after they are sent.
Thanks. I resolved the issue.
The place and parameter i was passing in response was incorrect.
Correct code -
session
.run(` MATCH p=()-[r:Parameter]->() RETURN p ;`)
.then(function (result){
result.records.forEach(function(record){
console.log("record = ", record);
console.log("result = ", result)
console.log("1] record._fields[0].properties = ",record._fields[0].end.properties);
// res.send(record);
});
res.send(result);
})
.catch(function(err){
console.log("inside catch = " + err);
})
session.close();
}
I am testing a basic aggregation function using counts from Sequelize and here's my type Counts:
type Creserve {
id: ID!
rDateStart: Date!
rDateEnd: Date!
grade: Int!
section: String!
currentStatus: String!
user: User!
cartlab: Cartlab!
}
type Counts {
section: String!
count: Int
}
type Query {
getBooking(id: ID!): Creserve!
allBookings: [Creserve]
getBookingByUser(userId: ID): Creserve
upcomingBookings: [Creserve]
countBookings: [Counts]
}
I am using countBookings as my query for aggregate functions and here's my resolver for the query:
countBookings: async (parent, args, {models}) =>
{
const res = await models.Creserve.findAndCountAll({
group: 'section',
attributes: ['section', [Sequelize.fn('COUNT', 'section'), 'count']]
});
return res.rows;
},
The query that it outputs is this:
Executing (default): SELECT "section", COUNT('section') AS "count" FROM "Creserve" AS "Creserve" GROUP BY "section";
And tried this query in my psql shell and it's working fine:
section | count
---------+-------
A | 2
R | 2
However, when I tried querying countBookings in my GraphQL Playground, section is returned but not the count:
{
"data": {
"countBookings": [
{
"section": "A",
"count": null
},
{
"section": "R",
"count": null
}
]
}
}
Is there something I missed out? Or is this a bug? This is the answer I tried following to with this example: https://stackoverflow.com/a/45586121/9760036
Thank you very much!
edit: returning a console.log(res.rows) outputs something like this:
[ Creserve {
dataValues: { section: 'A', count: '2' },
_previousDataValues: { section: 'A', count: '2' },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: [Object],
omitNull: false,
hooks: [Object],
sequelize: [Sequelize],
uniqueKeys: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array] },
__eagerlyLoadedAssociations: [],
isNewRecord: false },
Creserve {
dataValues: { section: 'R', count: '2' },
_previousDataValues: { section: 'R', count: '2' },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: [Object],
omitNull: false,
hooks: [Object],
sequelize: [Sequelize],
uniqueKeys: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array] },
__eagerlyLoadedAssociations: [],
isNewRecord: false } ]
Here's for res.count:
Executing (default): SELECT "section", COUNT('section') AS "count" FROM "Creserve" AS "Creserve" GROUP BY "section";
[ { count: '2' }, { count: '2' } ]
Problem
Actually you are doing everything right here... but what is happening here is the sequlize doesn't return plain object... It always returns the data in form of instance like that
[ Creserve {
dataValues: { section: 'A', count: '2' },
_previousDataValues: { section: 'A', count: '2' },
_changed: {},
_modelOptions:
{ timestamps: true,
Solution
I am not sure but there is no other way instead of looping and makes
response to json object...
const array = []
res.rows.map((data) => {
array.push(data.toJSON())
})
return array