Every time I try to assign a variable from a class I cannot use it.
Then when I make a small sample code to put on-line, the functions work in the small example. How do I log a variable to the screen after returning it from a class?
In the small code examples it works, in the larger code to actually do things, I receive an undefined reference error probably as the functions take longer to run.
With a main.js like:
var KeyPair = require('./Keypair');
var Q = require("q");
path_k = __dirname + "/user_p_key_1.txt";
var pair = new KeyPair();
var p = pair.getPublicKey(path_k);
console.log(p + " sig_1"); // error: logs "undefined sig_1" instead of key.
Q.fcall(p = pair.getPublicKey(path_k))
.then(console.log(p + " sig_2")); // error: logs "undefined sig_2" instead of key.
The output is:
undefined sig_1
undefined sig_2
Key fileread successfully
Is it a valid public key?
true
Key:GAVAPNTBWRG337JWZYLR2SBPD5KFA2PPXDMN4HEEBLWSZQIKTPOEFZL6
Key fileread successfully
Is it a valid public key?
true
Key:GAVAPNTBWRG337JWZYLR2SBPD5KFA2PPXDMN4HEEBLWSZQIKTPOEFZL6
Key fileread successfully
Is it a valid public key?
true
Key:GAVAPNTBWRG337JWZYLR2SBPD5KFA2PPXDMN4HEEBLWSZQIKTPOEFZL6
Using the Keypair.js Class:
var fs = require('fs');
var StellarBase = require('stellar-base');
var StellarSdk = require('stellar-sdk');
// A class for referencing KeyPair methods to read and write from files
var method = KeyPair.prototype;
function KeyPair() {
}
method.getPublicKey = function(path_k) {
// Read PublicKey from filename
fs.open(path_k, 'r', function(err, fd) {
if (err) {
throw 'could not open file: ' + err;
}
// Buffer Array Initialised for the Public Key
// write the contents of the buffer, from position 0 to the end, to the file descriptor returned in opening our file
var arr = [null,null,null,null,null,null,null,null,null,null,null,null,
null,null,null,null,null,null,null,null,null,null,null,null,
null,null,null,null,null,null,null,null];
var buffer_k = Buffer.from(arr);
fs.read(fd, buffer_k, 0, buffer_k.length, null, function(err) {
if (err) throw 'error reading file: ' + err;
fs.close(fd, function() {
//console.log('Key fileread successfully');
var user_encoded_k = StellarBase.StrKey.encodeEd25519PublicKey(buffer_k);
//console.log('Is it a valid public key?');
//console.log(StellarBase.StrKey.isValidEd25519PublicKey(user_encoded_k));
//console.log('Key:' + user_encoded_k);
return user_encoded_k
});
});
});
};
Reading in a Public Key "user_p_key_1.txt":
*¶a´M½ý6ÎH/TPiï¸ØÞ„
í,Á
݆B
The key is a for a test-server.
Also, I created the simplified Keypair.js as below to show that my code isn't working, and the sample Keypair.js worked different to the Keypair.js above and assign the variable and logged it to screen correctly.
Simplified Keypair.js
var fs = require('fs');
// A class for referencing KeyPair methods to read and write from files
var method = KeyPair.prototype;
function KeyPair() {
}
method.getPublicKey = function(value) {
return 1
};
module.exports = KeyPair;
I considered it was perhaps something to do with the asynchronous interpreter not being finished with the function when the variable is called so I implemented it afterwards in promises, and to my disbelief p
still returned undefined. How do we assign a variable from a class functions and use it in Node.JS?