0

I need to read my user Id to know what the timestart number is but I don't find the way to read the data.

//My code
const fs = require('fs');
var timePath = 'test.json'
var TimeRead = fs.readFileSync(timePath);
var timeFile = JSON.parse(TimeRead); //ready for use
var userId = "2"

var Time = TimeRead.userId.TimeStart; 

console.log(Time)

1 is my user Id variable

//My json File
{
  "1": {
    "TimeStart": 1626909816680
  },
  "2": {
    "TimeStart": 1627166305644
  }
}

3
  • Please say what the problem is. If the code you posted does not work, show the error or explain what is wrong. Commented Jul 26, 2021 at 15:36
  • your ignoring timeFile after decode, use that instead, timeFile[userId].TimeStart Commented Jul 26, 2021 at 15:36
  • Test question: Why do you assume that in TimeRead.userId.TimeStart the userId refers to 2 while assuming at the same time that TimeStart refers to "TimeStart"? Commented Jul 26, 2021 at 15:36

1 Answer 1

1
var Time = TimeRead.userId.TimeStart; 

You can't use a variable with .

Try this:

var Time = TimeRead[userId].TimeStart; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.