Sunday, 23 April 2017

can't read a file from tmp dir in Linux

I am writing an electron app which creates a file in user's system in tmp dir with an extension (.tms) and afterwords read that tmp dir and get all the files with an extension (.tms) and pick one file and read it's content and further parse the content using JSON.parse() . The files has contents in JSON string , so I need to parse that string obj. But my code read the dir like {/tmp/12321212.tms} but can't read the content from that dir. That's why JSON.parse function is giving error. Below is the piece of code.

function getScreenshotName (){
    return new Promise( (resolve,reject) =>{
        var files = readDir.readSync(os.tmpdir()+'/',["**.tms"])
        if (files.length > 0 ) {
            return resolve(files[0])
        }
        else{
            return reject(err)
        }
    })
}

function getScreenshotObj (pathToFirstFile) {
    return new Promise ((resolve,reject) =>{
        console.log("Path to temp dir : " + pathToFirstFile)
        fs.readFile(pathToFirstFile, 'utf8',function(err,fileContents){
            if (err) {
                return reject(err)
            }
            else{
                console.log("File contents : ")
                console.log(fileContents)
                screenshotObject = JSON.parse(fileContents)
                obj = {pathToFirstFile : pathToFirstFile , screenshotObject:screenshotObject ,accesstoken : accesstoken}
                return resolve(obj)
            }
        })
    })
}

Error is in JSON.Parse function because it can't read the contents .



via Raja

No comments:

Post a Comment