how to export this value in nodejs - javascript

I'm trying to get the amount received from the request by going to a list and then trying to export it to another file, but I'm having trouble doing this because I tried several ways to pass the list as return and it didn't work.
const moment = require("moment");
const sslChecker = require("ssl-checker");
const express = require("express");
//const dominios = require('./server');
var dominios = [
"api-prd.koerich.com.br",
"api-prd.reservapto.com.br",
"c.btg360.com.br",
"compassouol.com",
"gmfy.compasso.com.br",
"webqplus.cbn.com.bo",
"webqplus.cerchile.cl",
"webqplus.cervepar.com.py",
"webqplus.fnc.com.uy",
"webqplus.quilmes.com.ar",
"www.efurukawa.com",
];
var get_domain = dominios;
for (var i = 0; i < get_domain.length; i++) {
sslChecker(get_domain[i])
.then((certdata) => {
var startdate = new Date(certdata.validFrom);
var enddate = new Date(certdata.validTo);
var certstart = moment(startdate);
var certend = moment(enddate);
var ssldata = [
{
domain: get_domain,
issued: certstart.format("LLLL"),
expires: certend.format("LLLL"),
daysleft: certdata.daysRemaining,
},
];
var data = JSON.parse(certdata.daysRemaining);
var nomes = (certdata.validFor[0]);
var lista = [];
lista.push(data);
lista.push(nomes);
console.log(lista); //get this values
})
.catch((err) => {
console.log(err);
});
}
//module.exports = lista; //export here
console.log:

async function getList() {
mondongo = [];
var dominios = [
"api-prd.koerich.com.br",
"api-prd.reservapto.com.br",
"c.btg360.com.br",
"compassouol.com",
"gmfy.compasso.com.br",
"webqplus.cbn.com.bo",
"webqplus.cerchile.cl",
"webqplus.cervepar.com.py",
"webqplus.fnc.com.uy",
"webqplus.quilmes.com.ar",
"www.efurukawa.com",
]
try {
for (var i = 2; i < domains.length; i++) {
mondongo.push(
sslChecker(domains[i])
.then((certdata) => {
var startdate = new Date(certdata.validFrom);
var enddate = new Date(certdata.validTo);
var certstart = moment(startdate);
var certend = moment(enddate);
var ssldata = [
{
domain: domains,
issued: certstart.format("LLLL"),
expires: certend.format("LLLL"),
daysleft: certdata.daysRemaining,
},
];
var data = certdata.daysRemaining;
var nome = certdata.validFor[0];
var lista = [];
lista.push(nome);
lista.push(data);
return lista;
})
.catch((err) => {
//console.log(err);
})
);
}
}
catch (error) {
console.log(error);
}
var arroz = await Promise.all(mondongo);
return arroz;
}
return:

create a function and use the async await methods.

Related

Result won't update VAR

I am trying to run a query, inside AXIOS which gets data from a 3rd party URL. Then uses some of that data to search our mongoDB database.
However it seems it won't update var total = 0
While the query below does function correctly, the return result won't allow me to set that it to the query.
Promise.all(arr.forEach( async (id,index) => {
//(CODE REMOVED JUST TO GET THIS FUNCTION TO WORK)
const search = await geoLocation.find({
'location': {
'$geoWithin': {
'$box': [
[-35.2418503, -13.5076852], [112.8656697, 129.0020486]
]
}
}}).toArray();
total = search.length;
}));
See the full code below
var array = [];
var pointarray = []
var total = 0;
areas.forEach((id,index) => {
if(id.type == "Point"){
pointarray[index] = "N"+id.id;
}else{
array[index] = "R"+id.id;
}
});
var arraySearch = "https://nominatim.openstreetmap.org/lookup?osm_ids="+array.toString()+"&polygon_geojson=1&bbox=1&format=json";
var pointSearch = "https://nominatim.openstreetmap.org/lookup?osm_ids="+pointarray.toString()+"&polygon_geojson=1&bbox=0&format=json"
const requestOne = axios.get(arraySearch);
const requestTwo = axios.get(pointSearch);
axios.all([requestOne, requestTwo])
.then(axios.spread((...responses) => {
const responseOne = responses[0]
const responseTwo = responses[1]
/*
process the responses and return in an array accordingly.
*/
return [
responseOne.data,
responseTwo.data,
];
}))
.then(arr => {
Promise.all(arr.forEach( async (id,index) => {
//const middleIndex = id[index].boundingbox.length / 2;
//const firstHalf = id[index].boundingbox.splice(0, middleIndex);
//const secondHalf = id[index].boundingbox.splice(-middleIndex);
//res.send(secondHalf[0]);
const query = [{
$match: {
location: {
$geoWithin: {$box:[[Number(firstHalf[0]),Number(firstHalf[1])],[Number(secondHalf[0]),Number(secondHalf[1])]]
}
}
}
},{
$count: 'id'
}]
const search = await geoLocation.find({
'location': {
'$geoWithin': {
'$box': [
[-35.2418503, -13.5076852], [112.8656697, 129.0020486]
]
}
}}).toArray();
total = search.length;
// total = search.length;
// const search = geoLocation.aggregate(query).toArray.length;
}));
})
.catch(errors => {
console.log("ERRORS", errors);
})
.then(function () {
res.send(total);
});

how to save js object to JSON format

I want to save my JavaScript object to a JSON file. I already did like this
const superagent = require('superagent');
const cheerio = require('cheerio');
const fs = require('fs');
var object = new Object();
var toJson = { articles: [] };
var arr = [];
// I obtain the data prior to this forEach loop.
// The data loading code is omitted, because it is too long to fit here.
data.forEach((val, index) => {
const authorName = val.authorName;
const articleDate = val.articleDate;
const relateArticle = val.relateArticle;
const relateArticleURL = val.relateArticleURL;
object.url = arr[1][index];
object.title = arr[0][index];
object.date = articleDate[0];
object.author = authorName[0];
toJson.articles.push(object);
});
var saveJson = JSON.stringify(toJson)
fs.writeFile('solution.json', saveJson, 'utf8', (err) => {
if (err) {
console.log(err)
}
})
I would expect the result to look like so:
{
"articles": [
{
"url": "...",
"title": "...",
"author": "...",
"postingDate: "..."
}
]
}
but what I get instead looks like so:
{"articles":[{"url":"...","title":"...","author":"...","postingDate":"..."}]}
How do I save an object to a JSON file, but in the desired format? Any answer would be appreciated. Thank you in advance!
Try this
const superagent = require('superagent');
const cheerio = require('cheerio');
const fs = require('fs');
var object = new Object();
var toJson = { articles:[] };
var arr = [];
// above this for each, is how i get the data. i don't put those code because it's too long.
data.forEach((val, index)=>{
const authorName = val.authorName;
const articleDate = val.articleDate;
const relateArticle = val.relateArticle;
const relateArticleURL = val.relateArticleURL;
object.url = arr[1][index];
object.title = arr[0][index];
object.date = articleDate[0];
object.author = authorName[0];
toJson.articles.push(object);
});
var saveJson = JSON.stringify(toJson, null, 4)
fs.writeFile('solution.json', saveJson, 'utf8', (err)=>{
if(err){
console.log(err)
}
})

electron : Lodash not working in electron Main process

I'm downloading posts from subreddit
const electron = require('electron');
const _ = require('lodash');
const getsub = (sub, epoch) => {
let res = {};
const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${sub}&limit=1000&sort=desc&before=${epoch}`;
return new Promise((resolve, reject) => {
axios
.get(sub_url)
.then(response => {
let d = response.data.data;
let e = d[d.length - 1].created_utc;
res.data = d;
res.epoch = e;
return resolve(res);
})
.catch(error => {
return reject(error.message);
});
});
};
The function in question
async function getSubreddit(sub) {
const utc = new Date().toJSON().slice(0, 10).replace(/-/g, "/");
const endEpoch = new Date(utc).getTime() / 1000;;
var dataArray = [];
let epoch;
const resp = await getsub(sub, endEpoch);
const jsondat = resp.data;
epoch = resp.epoch;
var result = _.map(dataArray, function(o) {
return _.pick(o, ['title', 'url', 'subreddit', 'created_utc', 'domain', 'full_link', 'created_utc'])
})
console.log(jsondat.length);
// console.log({result,epoch});
// downloadStuff(sub,result)
}
while jsondat shows a length of 1000. But after I parse it with lodash, the result shows length as 0.
I'm confused because this code works in regular javascript page.
EDIT: This is a silly error on my part. Here is the corrected code
async function getSubreddit(sub) {
var utc = new Date().toJSON().slice(0, 10).replace(/-/g, "/");
var endEpoch = new Date(utc).getTime() / 1000;;
var dataArray = [];
const resp = await getsub(sub, endEpoch);
const jsondat = resp.data;
dataArray = jsondat.concat(dataArray);
epoch = resp.epoch;
var result = _.map(dataArray, function(o) {
return _.pick(o, ['title', 'author','url', 'subreddit', 'created_utc', 'domain', 'full_link', 'created_utc'])
})
console.log({result,epoch});
downloadStuff(sub,result)
}

Bitfinex API websocket orderbook

I am trying to get the order book through the bitfacex bitfex websocks, but I get an error in the console.
Error in the console: Cannot read property '0' of undefined on the line if (bidsKeys [i]) {
var ws = new WebSocket("wss://api-pub.bitfinex.com/ws/2");
ws.onopen = function() {
// ws.send(JSON.stringify({ event: 'conf', flags: 131072 }))
let msg = JSON.stringify({
event: 'subscribe',
channel: 'book',
pair: 'tBTCUSD',
freq: 'F1',
len: '25',
prec: 'P0'
})
ws.send(msg)
ws.onmessage = function(msg) {
const BOOK = {}
BOOK.bids = {}
BOOK.asks = {}
BOOK.psnap = {}
BOOK.mcnt = 0
msg = JSON.parse(msg.data)
// var response = JSON.parse(msg.data);
// console.log(msg.data);
const csdata = []
const bidsKeys = BOOK.psnap['bids']
const asksKeys = BOOK.psnap['asks']
for (let i = 0; i < 25; i++) {
if (bidsKeys[i]) {
const price = bidsKeys[i]
const pp = BOOK.bids[price]
csdata.push(pp.price, pp.amount)
}
if (asksKeys[i]) {
const price = asksKeys[i]
const pp = BOOK.asks[price]
csdata.push(pp.price, -pp.amount)
}
}
const csStr = csdata.join(':')
const csCalc = CRC.str(csStr)
if (csCalc !== checksum) {
console.error('CHECKSUM_FAILED')
}
}

I am trying to resolve the data in map function

Below is my code.I am unable resolve the o/p.How to resolve in async and await.I am trying to get data from other collection and pass it to existing filtered collection
exports.listStatus = async (root, { _id, start, limit }) => {
var user = await db.User.find({ _id: _id })
let list = user[0].savedUsers
var blocked = user[0].blockedUsers
var listAll = list.push(_id)
let listOfStatus = await db.Status.find({ userId: { $in: list }, reportStatus: { $ne: _id }, _id: { $nin: blocked } });
let array;
let leng = listOfStatus.length
let statusCommentsArray
var arrayStatus = [];
listOfStatus.forEach((status, key) => {
let promise = new Promise((resolve, reject) => {
var obj = {}
var statusId = status._id
var listOfLiked = status.like
let likeCount = listOfLiked.length
var sortedList = listOfLiked.sort({ createdDate: -1 })
obj.likeCount = likeCount
db.Comment.find({ statusId: statusId }, (err, statusCommentsList) => {
var statusCommentsCount = statusCommentsList.length
var sortedList = statusCommentsList.sort({ createdDate: -1 })
if (statusCommentsCount >= 3) {
statusCommentsArray = sortedList
}
else {
statusCommentsArray = sortedList.slice(0, 3)
}
obj.statusCommentsCount = statusCommentsCount
obj.statusCommentsArray = statusCommentsArray
arrayStatus.push(obj)
leng--;
if (leng == 0) {
resolve(arrayStatus)
}
})
})
promise.then((response) => {
return Promise.resolve(response)
})
})
}

Categories

Resources