typescript method returning promise doesnt return a object always.

typescript method returning promise after reading data from a file doesn't return object always.

   
 public getData(): Promise<node[] > {
    // read file line by line
    let lineReader = require("line-reader");
    let dataTempJson = "";
    let dataJson = "[{";
    let quote = "\"";
    let cont = -1;
    let contData = -1;
    let cache = "";
    let role = "";
    let resultData;
    try {

        let fs = require("fs");
        const path = require("path");
        lineReader.open(path.join("log", "data.log"), (err: Error, reader: LineReader) => {
            if (err) {throw err; }
            if (reader.hasNextLine()) {
                try {
                    reader.nextLine((error: Error, line: string) => {
                        if (error) {throw error; }
                    });
                } finally {
                    reader.close( (error: Error) => {
                        if (error) {throw error; }
                    });
                }
            } else {
                reader.close((error: Error) => {
                    if (error) {throw error; }
                });
            }
        });
        lineReader.eachLine(path.join("logs", "data.log"), { encoding: "utf8" }, (line, isLast) => {
            let keyLen = line.indexOf(": ") + ": ".length;
            let valueLen = line.indexOf(";");
            cont = line.indexOf("\\");
            contData = line.indexOf(",");
            if (cont > 0) {
                cache = quote + line.substring(0, keyLen - 2).trim() + quote + ":" + quote;
                cont = 0;
            } else if (contData > 0) {
                cache = quote + line.substring(0, keyLen - 2).trim()
                    + quote + ": " + quote + line.substring(keyLen).trim();
            } else {
                let data = "";
                if (cache.length > 0) {
                    data = cache + line.substring(0, line.length - 1).trim() + quote + ",";
                    cache = "";
                } else {
                    let key = quote + line.substring(0, keyLen - 2).trim() + quote;
                    let value = quote + line.substring(keyLen, valueLen).trim() + quote;
                    if (line.substring(0, keyLen - 2).trim()) {
                        data = key + ":" + value + ",";
                    }
                }
                dataTempJson = dataTempJson + data;
                if (line.indexOf("end") >= 0) {
                         dataJson = dataJson
                        + dataTempJson.substring(0, dataTempJson.length)
                        + ipaddress
                        + addParams
                        + "},{";
                    dataTempJson = "";
                    cache = "";
                    contData = 0;
                    cont = 0;
                }
            }
            if (isLast) {
                // tslint:disable-next-line:no-console
                console.log(dataJson.substring(0, dataJson.length - 2) + "]");
                resultData = JSON.parse(dataJson.substring(0, dataJson.length - 2) + "]");
                // tslint:disable-next-line:no-console
                console.log(resultData);
        }
        });
    } catch (error) {
        return error;
    }
    // Return new promise
    return new Promise((resolve, reject) => {
        if (resultData) {
            resolve(resultData);
        } else {
            reject("error");
        }
    });

Open in new window

Srilalitha vishnubhotlaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
linereader is asynchronous, you should use promise not at the end , check promisify : https://github.com/nickewing/line-reader#promises
1
 
leakim971PluritechnicianCommented:
what did it return when it's not an object ? undefined ?
0
 
Srilalitha vishnubhotlaAuthor Commented:
yes undefined.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Srilalitha vishnubhotlaAuthor Commented:
Changed code.  returns promise in the beginning of function.  It can be closed.
0
 
leakim971PluritechnicianCommented:
if your issue is solved, please close your question by selecting ans accept one or more answer as solution.
0
 
Srilalitha vishnubhotlaAuthor Commented:
Issue is closed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.