0

I need to compare 2 company names, one is from a text file, the other from databases. I import a licence in txt file and if the names are different, a notification on page will pop up asking if I want to rename it (new name will be saved in database).

In JavaScript React, I have to write code that can read a text file, specifically one line from it. This line contains the name of the company that I need to compare with the second name which is in the database.

const handleDispalyDialogButtonClick = () => {
  if (nametxt! == namedatabase) {
    setDisplayDialog(true)
  }
  handleImportButtonClick();
};

nametxt is to be the name of the company in the text file. This name needs to be imported somehow and assigned to nametxt.

[license]
lic_base=xxxx
lic_nip=xxxx
PC_Nazwa1=name of the company1
PC_Nazwa2=name of the company2
PC_Nazwa3=name of the company3

For example it's like Car=BMW.

Does anyone have a solution how to make a code which gonna import information, and assign it?

1 Answer 1

1

You need to put the file you want in your public folder and just fetch it:

fetch('/path/to/the/file').then((response) => response.text())
    .then((data) => {
    // read file lines here
    const fileLines = data.split('\n')
    // find the value you are looking for and compare it to the value you get from the database
}

Also, the screenshot you shared looks like a TOML file, you might want to parse that with TOML Parser to make things easier.

For the value on the database, you cannot directly interact with a database from your code. You need to have an extra layer like an API and request the data from there.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, unfortunately it doesn't work properly. When I check the console, I dont have any text that is inside of txt file.
Can't really help without more information. You might want to check the network tab in developer tools to see if the file is being downloaded.

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.