I'm going crazy - the solution is probably right in front of me - but ...
const moment = require('moment')
moment.locale('nb')
let dato = moment(Data.datoen)
let rettDatoStart = moment.utc(dato).startOf('day').toDate()
let rettDatoSlutt = moment.utc(dato).endOf('day').toDate()
... logs out as:
2020-08-05
2020-08-06T00:00:00.000Z
2020-08-06T23:59:59.999Z
For the record, this is written 2020-08-06T22:57:00:000Z
What's wrong? Anyone?
EDIT:
I dawns on me that there is nothing wrong with this code, nor with the first suggestion. The problem, I suspect, is that this snippet is part of an async operation inside an express.js server. I don't see why it's a problem, but some of you might?
Service.js:
const General = require('../../database/models/hotels/generalModel')
const Data = require('../../controller/hotels/generalController')
const moment = require('moment')
moment.locale('nb')
module.exports.getAllGeneral = async (serviceData) => {
try {
let dato = moment(Data.datoen)
let rettDatoStart = moment.utc(dato).startOf('day').toString()
let rettDatoSlutt = moment.utc(dato).endOf('day').toString()
let generalen = await General.find({
skaptdato: {
$gte: rettDatoStart,
$lte: rettDatoSlutt
}
});
return generalen;
} catch (error) {
console.log('Noe gikk galt: Service: getAllGeneral', error);
throw new Error(error);
}
}
Controller.js:
const generalService = require('../../service/hotels/generalService');
module.exports.getAllGeneral= async (req, res) => {
// Hent datoen fra urlen og exporter den.
const datoen = req.query.dato;
exports.datoen = datoen;
let response = {};
try {
const responseFraService = await generalService.getAllGeneral(req.body);
response.datoen = datoen;
response.status = 200;
response.message = 'General Hotell er hentet! [controller]';
response.body = responseFraService;
} catch (error) {
console.log('Noe gikk galt: Controller: getAllGeneral', error);
response.status = 400;
response.message = error.message;
response.body = {};
}
return res.status(response.status).send(response);
}
EDIT #2:
Strangely,
let dato = moment(Data.datoen).add(1, 'day')
... provides the expected output - but this makes zero sense to me.
Related
I have been trying to send the var logtraceArr = []; value to the browser using Expressjs but one first call empty array is displayed on 2nd call array is filled with first called function and on 3rd call array is appended with the same element.
I want the content to be displayed on the first.
var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const directoryPath = path.resolve(__dirname,"../logtrace-filestore/edepoze-logs");
var logtraceArr = [];
router.get("/",function(req,res,next){
const fetchFileContentParsed = async (fileContentObj) => {
var logtraceObj = {
Direction : '',
FromServer: '',
ToServer:''
};
var pipearray = fileContentObj.toString().split("|");
for( i=0;i<pipearray.length;i++){
var Direction = new Array();
Direction = pipearray[5].split("::");
if (Direction.length == 2) {
logtraceObj.Direction = Direction[1];
} else {
logtraceObj.Direction = "";
}
}
logtraceArr.push(logtraceObj);
return {
logtraceArr
}
}
// Iterates all users and returns their Github info.
const getfileContent = async (files) => {
const fileContent = files.map((file) => {
const filePath = path.join(directoryPath,file);
//console.log("filePath ----->",filePath);
const readStream = fs.createReadStream(filePath);
const fileContent = readline.createInterface({
input: readStream
});
fileContent.on('line', function(line) {
const regExp = /\[Event([^]+\])/g
var matchedContent;
if ((matchedContent = line.match(regExp)) != null){
return fetchFileContentParsed(matchedContent) // Async function that fetches the user info.
.then((a) => {return a })
}
})
})
return Promise.all(fileContent) // Waiting for all the requests to get resolved.
}
function readFiles(dirname) {
//console.log(dirname);
fs.readdir(dirname, async function (err,filenames)
getfileContent(filenames).then((a) => {return a })
});
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
//console.log(readFiles(directoryPath))
readFiles(directoryPath,logtraceArr);
res.send(logtraceArr);
});
module.exports = router;
i want the content to be loaded and print the array only once on refresh of the page
var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const directoryPath = path.resolve(__dirname,"../logtrace-filestore/edepoze-logs");
var logtraceArr = [];
router.get("/",function(req,res,next){
const fetchFileContentParsed = async (fileContentObj) => {
var logtraceObj = {
Direction : '',
FromServer: '',
ToServer:''
};
var pipearray = fileContentObj.toString().split("|");
for( i=0;i<pipearray.length;i++){
var Direction = new Array();
Direction = pipearray[5].split("::");
if (Direction.length == 2) {
logtraceObj.Direction = Direction[1];
} else {
logtraceObj.Direction = "";
}
}
logtraceArr.push(logtraceObj);
return {
logtraceArr
}
}
// Iterates all users and returns their Github info.
const getfileContent = async (files) => {
const fileContent = files.map((file) => {
const filePath = path.join(directoryPath,file);
//console.log("filePath ----->",filePath);
const readStream = fs.createReadStream(filePath);
const fileContent = readline.createInterface({
input: readStream
});
fileContent.on('line', function(line) {
const regExp = /\[Event([^]+\])/g
var matchedContent;
if ((matchedContent = line.match(regExp)) != null){
return fetchFileContentParsed(matchedContent) // Async function that fetches the user info.
.then((a) => {return a })
}
})
})
return Promise.all(fileContent) // Waiting for all the requests to get resolved.
}
function readFiles(dirname) {
//console.log(dirname);
fs.readdir(dirname, async function (err,filenames)
getfileContent(filenames).then((a) => {return a })
});
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
//console.log(readFiles(directoryPath))
readFiles(directoryPath,logtraceArr);
res.send(logtraceArr);
});
module.exports = router;
I have been trying to send the var logtraceArr = []; value to the browser using Expressjs but one first call empty array is displayed on 2nd call array is filled with first called function and on 3rd call array is appended with the same element.
I want the content to be displayed on the first.
You define logtraceArr in the global scope, so it never will be cleared. If you want to have an empty array for each request - define logtraceArr inside the router callback (for example on the line before calling readFiles)
Promise.all() doesn't wait if you don't await it. To wait you can return result of getfileContent from readFiles and then send request after result of readFiles. Fof example:
async function readFiles(dirname) {
fs.readdir(dirname, async function (err,filenames)
await getfileContent(filenames).then((a) => {return a })
});
return
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
readFiles(directoryPath,logtraceArr).then(() => res.send(logtraceArr));
I have a nodejs/express backend api that when I get hundreds of requests within seconds I start to get mixed results where data is crossed between requests and leads to unexpected and incorrected results. Can someone point me to where I am incorrectly defining variables that when I get one of the await functions it causes the other variables to be overwritten with the next requests data and causing my issues.
router.post('/autoscan3', async(req,res,next) => {
let authrule = "autoscan"
console.log(req.body.companyDomain + " " + req.body.user + " for folder: " + req.body.folderid)
let totalscans = 0;
let client = await getDB();
let authorizationResults = await checkAuthorization(client, req.body.companyDomain);
const featureSet = authorizationResults.features;
let company = authorizationResults.company;
const clientid = authorizationResults.client_id;
const clientsecret = authorizationResults.secret;
let tenantid = await getTenantID(req.body.companyDomain, client);
let token = await msgraphservices.getClientCredentialsAccessToken(tenantid, clientid, clientsecret)
let scanResults = []
let folderscans = 0;
try{
for(let a = 0; a < req.body.messages.length; a++){
let senderAddress = req.body.messages[a].senderAddress;
let emailBody = req.body.messages[a].emailBody;
let messageid = req.body.messages[a].messageid;
let receivedDateTime = req.body.messages[a].receivedDateTime;
let user = req.body.messages[a].recieverAddress;
let subject = req.body.messages[a].subject;
let attachments = req.body.messages[a].attachments;
let links = req.body.messages[a].emailBodyLinks;
let headers = req.body.messages[a].headers
let knownaddress
if (senderAddress.includes(req.body.companyDomain)) {
knownaddress = 10
} else {
knownaddress = await searchForSender(client, senderAddress, req.body.user, token, "");}
let assessmentResults = await assessment(
messageid,
emailBody,
senderAddress,
user,
subject,
receivedDateTime,
company,
attachments,
links,
req.body.companyDomain,
client,
headers,
knownaddress,
featureSet,
authrule
)
console.log('adding to folderscans')
folderscans++
try {
await msgraphservices.updateUserCategory2(messageid, req.body.user, req.body.folderid, assessmentResults.riskFactor,token)
}
catch(e) {
console.log(`error on category tag for ${messageid} with user ${req.body.user}`);
console.log(e);
}
}
console.log(`folder scans ${folderscans}`);
totalscans = totalscans + folderscans
return res.status(200).json({status:"success", totalscans:totalscans});
}
catch(e) {
console.log(`error while trying to loop for user ${req.body.user}`)
console.log(e)
logapierror(e.stack, req.body.user, req.body)
return res.status(200).json({status:'error', totalscans:totalscans, error:e});
}
});
It's very likely let client = await getDB();
If multiple requests use the same snapshot of the database but don't know about each other, it's very likely that they're overwriting each other's data.
I'm trying to have some sort of real-time dashboard that can subscribe to AWS IoT core topics and maybe publish too.
I have found a couple of items online, but I can't figure it out.
This is what I currently have:
function p4(){}
p4.sign = function(key, msg) {
const hash = CryptoJS.HmacSHA256(msg, key);
return hash.toString(CryptoJS.enc.Hex);
};
p4.sha256 = function(msg) {
const hash = CryptoJS.SHA256(msg);
return hash.toString(CryptoJS.enc.Hex);
};
p4.getSignatureKey = function(key, dateStamp, regionName, serviceName) {
const kDate = CryptoJS.HmacSHA256(dateStamp, 'AWS4' + key);
const kRegion = CryptoJS.HmacSHA256(regionName, kDate);
const kService = CryptoJS.HmacSHA256(serviceName, kRegion);
const kSigning = CryptoJS.HmacSHA256('aws4_request', kService);
return kSigning;
};
function getEndpoint() {
const REGION = "eu-west-1";
const IOT_ENDPOINT = "blablablabla-ats.iot.eu-west-1.amazonaws.com";
// your AWS access key ID
const KEY_ID = "My-key";
// your AWS secret access key
const SECRET_KEY = "my-access-token";
// date & time
const dt = (new Date()).toISOString().replace(/[^0-9]/g, "");
const ymd = dt.slice(0,8);
const fdt = `${ymd}T${dt.slice(8,14)}Z`
const scope = `${ymd}/${REGION}/iotdevicegateway/aws4_request`;
const ks = encodeURIComponent(`${KEY_ID}/${scope}`);
let qs = `X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=${ks}&X-Amz-Date=${fdt}&X-Amz-SignedHeaders=ho
const req = `GET\n/mqtt\n${qs}\nhost:${IOT_ENDPOINT}\n\nhost\n${p4.sha256('')}`;
qs += '&X-Amz-Signature=' + p4.sign(
p4.getSignatureKey( SECRET_KEY, ymd, REGION, 'iotdevicegateway'),
`AWS4-HMAC-SHA256\n${fdt}\n${scope}\n${p4.sha256(req)}`
);
return `wss://${IOT_ENDPOINT}/mqtt?${qs}`;
}
// gets MQTT client
function initClient() {
const clientId = Math.random().toString(36).substring(7);
const _client = new Paho.MQTT.Client(getEndpoint(), clientId);
// publish method added to simplify messaging
_client.publish = function(topic, payload) {
let payloadText = JSON.stringify(payload);
let message = new Paho.MQTT.Message(payloadText);
message.destinationName = topic;
message.qos = 0;
_client.send(message);
}
return _client;
}
function getClient(success) {
if (!success) success = ()=> console.log("connected");
const _client = initClient();
const connectOptions = {
useSSL: true,
timeout: 3,
mqttVersion: 4,
onSuccess: success
};
_client.connect(connectOptions);
return _client;
}
let client = {};
function init() {
client = getClient();
client.onMessageArrived = processMessage;
client.onConnectionLost = function(e) {
console.log(e);
}
}
function processMessage(message) {
let info = JSON.parse(message.payloadString);
const publishData = {
retailer: retailData,
order: info.order
};
client.publish("sc/delivery", publishData);
}
$(document).ready(() => {
init();
client.subscribe("sc/orders/");
});
I keep getting the same error; AMQJS0011E Invalid state not connected., but I do see in the requests pane of chrome that there is an connection or so... What am I doing wrong here?
I don't see any of the logs anyhow...
I have put the code below my for a simple weather app using Open Weather
The code to get the icon code from the JSON works in the first block but not in the second?
i can console log the variable no problem but when i am trying to display the icon it isnt loading the png file?
The line in question is
let icon1 = jsonResponse.list[8].weather[0].icon
nextDayWeather.src = `/resources/icons/${icon1}.png`
full code for reference including same code working in an earlier function
async function getWeather(input) {
const city = input
try {
const response = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=`)
if (response.ok) {
const jsonResponse = await response.json()
const {icon} = jsonResponse.weather[0]
currTemp.innerText = Math.floor(jsonResponse.main.temp - 273)
currWind.innerText = jsonResponse.wind.speed
currWeather.src = `/resources/icons/${icon}.png`
return
}
throw new Error('Request Failed')
} catch(error) {
window.location = '/'
}
}
async function extraWeather(input) {
const city = input
try {
const response = await fetch(`http://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=`)
if (response.ok){
const jsonResponse = await response.json()
let icon1 = jsonResponse.list[8].weather[0].icon
let icon2 = jsonResponse.list[16].weather[0].icon
let icon3 = jsonResponse.list[24].weather[0].icon
nextDayWeather.src = `/resources/icons/${icon1}.png` //needs fixing??
nextDayWind.innerText = jsonResponse.list[8].wind.speed
nextDayTemp.innerText = Math.floor(jsonResponse.list[8].main.temp - 273)
nextDayWeather2.src = `/resources/icons/${icon2}.png`
nextDayWind2.innerText = jsonResponse.list[16].wind.speed
nextDayTemp2.innerText = Math.floor(jsonResponse.list[16].main.temp - 273)
nextDayWeather3.src = `/resources/icons/${icon3}.png`
nextDayWind3.innerText = jsonResponse.list[24].wind.speed
nextDayTemp3.innerText = Math.floor(jsonResponse.list[24].main.temp - 273)
console.log(jsonResponse.list[16].weather[0].icon)
return
}
throw new Error ('error') }
catch(error){
console.log(error)
}
}