Connecting my logic between two js files

ok i am actually making progress… in my App.js file i have a class “App” and in that I have a method “getAttemptedUser” that renders the screen does a text input and gets the name of the attempted user after user hits the carriage return (onsubmit event). It confirms it gets the name with an alert on the onsubmit method.

in another file I have the server listening on port 3000 for requests against my mongo db. I want to take the attempted user’s inputted name and in the other file, be able to put it into the find method for the Registered user database check.

I don’t know how to get the variable for the inputted value over from one file to the other. I’m thinking maybe i can require the App.js file, or maybe i can import something… i am not sure, hence my question.

I appreciate any insight any of you might provide.

Thankyou,
Biz

Biz - I don’t fully understand your setup, but if you have a function/method in a js file that exports itself and returns something, you can import it anywhere else and use it as if it was in the file doing the importing.

For example, I recently created a utility file that takes string values for hours, minutes and AM or PM (as arguments) and returns a date/time string compliant with ISO 8601.

In the component file where I’m using it, I assign it to a variable (to accept the returned value) and provide it with arguments (in this case, from state).

The import statement:
import isoCreate from "../components/Isocreate";

Here’s the call to the utility function:
let timefromuser = isoCreate(utime.hr, utime.min, ampmValue);

The timefromuser variable is now an ISO-compliant string that I use where needed. (And, the utility file function is easily used again in the future.)

Hope this helps.

Dave

Is that server on port 3000 an http server? Maybe your App.js client can send the username to your server via an http POST. Something like this.

Yes my friend, this is the goal and I can to that… however, my issue is that i can’t get the attemptedUser’s name so I can do the post.find request against my db of Registered users. No matter what i try to do to import it, i can’t get it.

Whenever i try to use modules and i export or export default a class, when i try to import in another module i get “Cannot use import statement outside a module.”

I put type: “module” in my script in package.json, it didn’t help. Even when I try Mosh’s example of person and teacher, it doesn’t work and gives me error on the Import statement.

I am stuck but i thank you of course for your help.

BB

Do you have some example code of what you’re trying to do?

E thanks for your interest in helping me. I was able to solve some of the import issues by learning more about modules. There are still some issues with import and export that I don’t understand and the differences in syntax with common vs. es6 continue to challenge me when i mix them up. Latest challenge is that I have an object person and an array attribute for Loves, meaning an array of also persons who a person cares for. However my addtoLoves method doesn’t check to verify if any string giberish i want to add to a person’s love list is actually a person. How do i access the instances of a class so I can verify that a person can have only legit other person objects in their list of people they care about?