Click here to Skip to main content
15,903,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Read the file from node and send it to the client as json. If the file exists, it works normally, but if the file cannot be read, an error is generated. What is causing the problem and how can I fix it?

res.json([{
             result:resData
        }]);

Of course you can do this.

res.json(resData);


However, in this case, the client JavaScript code needs to check the object before reading it. I think this is an unnecessary expense.

What I have tried:

Ajax
const jsonData = {'pID': id, lang: language };

$.getJSON(url, jsonData, function(result){
    page = new pageData();
    page.data = result[0].result;
    let text = result[0].content;

    let( text !== '' || text !== undefined ){
        /* Uncaught SyntaxError: Unexpected token u in JSON at position 0 */
        text = JSON.parse( text ); 
        subtext = text.subscribe;
    }
});

Node.js
let prevInstance = {};
app.get('/server', function(req, res){

   let num = req.query.pID;
   let resData = {};

   resData = prevInstance[num].getData(num++);

   /* dir file */
   let json_dir = '/local/jst.json';

   if(fs.existsSync(json_dir){
       let data = fs.readFile(json_dir, 'utf8', (err, data)=>{
           /* correct result */
           res.json({
               content:data,
               result:resData,
            }]);
        });
    }else{
        /* error */        
        res.json([{
             result:resData
        }]);
     }
});


VM19127:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
Posted
Updated 20-May-22 0:55am
v2
Comments
Richard MacCutchan 20-May-22 3:02am    
"if the file cannot be read, an error is generated. What is causing the problem and how can I fix it?"
How can anyone here guess what the error is? Please, again, provide proper details of what errors are occurring.
Chopin2001 20-May-22 5:05am    
sorry. I am having difficulties because English is not my mother tongue.
If the file is in the json_dir path, the file is read and returned without errors. But if the file doesn't exist, ajax throws an error.
Richard MacCutchan 20-May-22 5:33am    
What error? You need to check what you are passing in the json data when the file does not exist.
Chopin2001 20-May-22 6:11am    
It seems to be a check issue.
The return value is undefined, why is this being executed?
let text = result[0].content
console.log(text) <== undefined
if( text !== '' || text !== undefined ){
test = JSON.parse( text ); <<== error.
}
Richard MacCutchan 20-May-22 6:26am    
I cannot reproduce the situation you have there. But I would suggest that your test is wrong, and should be:
if( text !== undefined && text !== ''){

You need to check for both situations rather than either one. Also, in your original code you have
let( text !== '' || text !== undefined ){

rather than if.

1 solution

The correct expression to test for an undefined or empty value is:
JavaScript
if( text !== undefined && text !== ''){

Test for both conditions, and make sure undefined is first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900