'Undefined is not a function' in momentjs function DIFF - node.js

I have the following code:
var dateFormat = 'YYYY-MM-DD HH:mm:ss';
var time_margin = 10;
var last_message = moment().format(dateFormat);
var comparison = moment(last_message).add(time_margin, 'seconds').format(dateFormat);
var actualtime = moment().format(dateFormat);
var secondsDiff = actualtime.diff(comparison, 'seconds');
console.log("secondsdiff",secondsDiff);
It crashes right in var secondsDiff = actualtime.diff(comparison, 'seconds'); with Missing error handler on "socket".
TypeError: undefined is not a function.
comparison 2015-04-12 18:00:41
actualtime 2015-04-12 18:00:42
What might be wrong? I'm really not understanding

The problem is that you are trying to call diff on a string. When you call moment().format(dateFormat), what you have as result is a string, not an instance of moment.
In order to fix it, you need to call diff without formatting:
var dateFormat = 'YYYY-MM-DD HH:mm:ss';
var time_margin = 10;
var last_message = moment().format(dateFormat);
var comparison = moment(last_message).add(time_margin, 'seconds').format(dateFormat);
var secondsDiff = moment().diff(comparison, 'seconds');
console.log("secondsdiff",secondsDiff);
// => secondsdiff -9

Related

error by using in PDFTron:' NetworkError(`Unsupported protocol ${this._url.protocol}`'

I trying to convert pdf file to pdfa3 file by using PDFTron.
I added current url_path.
the my code below:
var input_url = './utils/';
var input_filename = 'test.pdf';
var output_filename = 'test_pdfa.pdf';
var convert = true;
var pwd = '';
var exceptions;
var max_ref_objs = 10;
var url_input = input_url + input_filename;
console.log('Converting input document: ' + input_filename);
var pdfa = await PDFNet.PDFACompliance.createFromUrl(true, url_input, '', PDFNet.PDFACompliance.Conformance.e_Level2B, exceptions, max_ref_objs);
get error:
'NetworkError(Unsupported protocol ${this._url.protocol})',
Does anyone know what the problem is,
And why doesn't it recognize the location?
I changed the code to :
here.
Now it's working!!

escodegen.generate throws Error: Unknown node type: undefined

The following is the code that I have written
`js
var esprima = require('esprima');
var escodegen = require('escodegen');
var a = "var a = 2";
var ast = esprima.tokenize(a);
var output = escodegen.generate(ast);
console.log(output);
`
I am able to tokenize the code string but I am getting error generating the code back. I went through multiple samples, Everywhere the same pattern is followed. I don't understand what I am doing wrong.
The function esprima.tokenize does not generate an AST, just an array of tokens. What you want to use is esprima.parse.
Try this:
var esprima = require('esprima');
var escodegen = require('escodegen');
var a = "var a = 2";
var ast = esprima.parse(a);
var output = escodegen.generate(ast);
console.log(output);
It will work

Failing to parse results of the nodejs function to specific varaibles

code...needed to pass the first set of results of the function obj to console. It returns nothing
var gplay = require('google-play-scraper');
var obj = gplay.reviews({appId: 'com'});
var Fieldsomevar = someVar.gplay.reviews[0][0];
console.log(Fieldsomevar);

base64 encode, nodejs c++ python's result is diffrent

nodejs:
var test = 'VdEU+Q2J5qfwsn9xshAcEImDSnxTR8RkRLlLmyNaeos=';
var result = new Buffer(test, 'base64').toString()
//var buf = Buffer.from(test, 'base64');
//var result = new Buffer(test, 'base64').toString("utf8");
var fs = require('fs');
fs.writeFile("test.txt",result,function(e){//会先清空原先的内容
if(e) throw e;
})
python:
import base64
result = base64.b64decode('VdEU+Q2J5qfwsn9xshAcEImDSnxTR8RkRLlLmyNaeos=')
file_object = open('thefile.txt', 'w')
file_object.write(result)
file_object.close( )
c++: (I use libcef's base library's base64):
const std::string kText = "VdEU+Q2J5qfwsn9xshAcEImDSnxTR8RkRLlLmyNaeos=";
std::string encoded;
base::Base64Decode(kText, &encoded);
FILE *pFile = fopen("1.txt", "wb+");
fwrite(encoded.c_str(), encoded.length(), 1, pFile);
fclose(pFile);
the result is python c++ is the same, but nodejs is different
You simply need to not call toString():
var result = new Buffer(test, 'base64');
Then the Node code does the same as the others.

Writing GPS exif data into image in Node

Good evening, community.
I have a question regarding changing exif meta data on jpegs using node.js. I have a set of coordinates which I need to attach to the image file, but for some reason, I cannot find the right library on npm for that. There are plenty of extracting libraries, like exif, exif-js, no-exif and so on, but all of the are retrieving data from images. I'm going the opposite direction, and extracting coordinates/gps data from the kml file and based on that updating the images, which do not have geo-location metadata.
What is the best approach for doing this?
I have written a library to modify exif on client-side. It would help you even on Node.js.
https://github.com/hMatoba/piexifjs
I tried to run the library on Node.js. No error occurred and got a new jpeg modified exif.
var piexif = require("piexif.js");
var fs = required("fs");
var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");
var exifObj = piexif.load(data);
exifObj["GPS"][piexif.GPSIFD.GPSVersionID] = [7, 7, 7, 7];
exifObj["GPS"][piexif.GPSIFD.GPSDateStamp] = "1999:99:99 99:99:99";
var exifbytes = piexif.dump(exifObj);
var newData = piexif.insert(exifbytes, data);
var newJpeg = new Buffer(newData, "binary");
fs.writeFileSync(filename2, newJpeg);
This worked best for me. eg. to write a coordinate of 23.2342 N, 2.343 W
const piexifjs = require("piexifjs");
var filename1 = "image.jpg";
var filename2 = "out.jpg";
var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");
var exifObj = piexif.load(data);
exifObj.GPS[piexif.GPSIFD.GPSLatitude] = degToDmsRational(23.2342);
exifObj.GPS[piexif.GPSIFD.GPSLatitudeRef] = "N";
exifObj.GPS[piexif.GPSIFD.GPSLongitude] = degToDmsRational(2.343);
exifObj.GPS[piexif.GPSIFD.GPSLongitudeRef] = "W";
var exifbytes = piexif.dump(exifObj);
var newData = piexif.insert(exifbytes, data);
var newJpeg = Buffer.from(newData, "binary");
fs.writeFileSync(filename2, newJpeg);
function degToDmsRational(degFloat) {
var minFloat = degFloat % 1 * 60
var secFloat = minFloat % 1 * 60
var deg = Math.floor(degFloat)
var min = Math.floor(minFloat)
var sec = Math.round(secFloat * 100)
deg = Math.abs(deg) * 1
min = Math.abs(min) * 1
sec = Math.abs(sec) * 1
return [[deg, 1], [min, 1], [sec, 100]]
}

Resources