0

I want to convert the following array (userNameApp) into string so I can insert the following array into the database as a string

        var userInfo = JSON.parse(window.localStorage.getItem('current-session'));
        const userNameApp = useState(userInfo['fname'] + " " + userInfo['lname']);
        console.log(userNameApp);

In the console log, the result is First Name + Last Name. When inserting in the database, I will post it like this

 Axios.post("http://localhost:3001/insertActiveUser", {userNameApp: userNameApp}))

Inside the database schema/model, the type of Value is string.

const mongoose = require("mongoose");
const User Details = new mongoose.Schema(
  {
    Username: String,
  },
  {
    collection: "AppointmentDetails",
  }
);
3
  • It's not clear to me what is the question... Commented Nov 2, 2022 at 14:16
  • I want to convert userNameApp into a string Commented Nov 2, 2022 at 14:19
  • You should read about useState Your wish of converting userNameApp into a string doesn't make any sense, because the second element of userNameAppis a function Commented Nov 2, 2022 at 14:27

2 Answers 2

1

The definition of userNameApp should be fixed to:

const [userNameApp, setUserNameApp] = useState(userInfo['fname'] + " " + userInfo['lname']);

Explanation

What returns from useState is an array of two values: the first value is the value we want to be able to set and use, and the second value is the set function.

If we'll want to re-set the variable userNameApp the correct way of doing it will be: setUserNameApp(newValue).

The value that we pass to useState will be the initial value.

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

1 Comment

you probably mean useState and not setState ...
0

What I did is

 var userInfo = JSON.parse(window.localStorage.getItem('current-session'));
 var getUserName = JSON.stringify(userInfo['fname'] + " " + userInfo['lname'])
 const userNameApp = JSON.parse(getUserName)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.