基本的 Express(React)用户POST请求

huangapple go评论122阅读模式
英文:

Basic post request for users using express (React)

问题

以下是代码部分的翻译:

class AddUsers extends React.Component {
  constructor() {
    super();

    this.state = { users: [] };
    this.onSubmit = this.handleSubmit.bind(this);
  }

  handleSubmit(e) {
    e.preventDefault();
    const data = { email: this.ref.email, password: this.ref.password };
    // const data = { name: "", password: "" };

    fetch("/admin-Add-Users", {
      method: "POST", // or 'PUT'
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
    })
      .then(response => response.json())
      .then(data => {
        console.log("Success:", data);
      })
      .catch(error => {
        console.error("Error:", error);
      });
  }

  render() {
    console.log(this.state.users);
    return (
      <div>
        <LoginForm></LoginForm>

        <form onSubmit={this.onSubmit}>
          <input type="text" placeholder="email" ref="email" />
          <input type="text" placeholder="password" ref="password" />
          <input type="submit" />
        </form>
      </div>
    );
  }
}

这是我的翻译。如果您需要更多帮助,请告诉我。

英文:

I am having serious issues trying to solve this issue and any help would be appreciated greatly

So all I am trying to do is a simple register activity for users where I will be able to sign them up to the site.

I am using mssql, and express.

This is My Register.js. All I want is for the details input into the buttons to be passed through to the json body so it can then be used in my server.js.

Register.js

class AddUsers extends React.Component {
constructor() {
super();
this.state = { users: [] };
this.onSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e) {
e.preventDefault();
const data = { email: this.ref.email, password: this.ref.password };
// const data = { name: "", password: "" };
fetch("/admin-Add-Users", {
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log("Success:", data);
})
.catch(error => {
console.error("Error:", error);
});
}
render() {
console.log(this.state.users);
return (
<div>
<LoginForm></LoginForm>
<form onSubmit={this.onSubmit}>
<input type="text" placeholder="email" ref="email" />
<input type="text" placeholder="password" ref="password" />
<input type="submit" />
</form>
</div>
);
}
}

This is my server.js (config file is working). Here all I want is for the data previously added to be stored in my database (SQL server).


app.post("/admin-Add-Users", function(req, res) {
const { password, email } = req.body;
var request = new sql.Request();
// query to the database and get the records
request.query( "insert into Login (email, password) values ('"+email+"','"+password+"')", function(err, recordset) {
if (err) console.log(err);
});
res.send({ message: "Success" });
});

I have no idea how to get the data from the inputs to just be stored through my server.js. Please any help or examples are appreciated. I am new to react so please explain like I am five years old.

Error I am now receiving

Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the method `isPropagationStopped` on a released/nullified synthetic event. This is a no-op function. If you must keep the original synthetic event around, use event.persist().

答案1

得分: 1

以下是您要翻译的内容:

"You should try to avoid use refs in react unless you have a good reason to use them (some things like animations need to be controlled imperatively).

The React way is to do things declaratively with state, so changing an input updates the associated state field, and then the onSubmit function takes the values from state. Something like this:

class AddUsers extends React.Component {
  constructor() {
    super();

    this.state = { users: [], email: '', password: '' };
    this.onSubmit = this.handleSubmit.bind(this);
  }

  handleSubmit(e) {
    e.preventDefault();
    const data = { email: this.state.email, password: this.state.password };

    fetch("/admin-Add-Users", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
    })
      .then(response => response.json())
      .then(data => {
        console.log("Success:", data);
      })
      .catch(error => {
        console.error("Error:", error);
      });
  }

  render() {
    console.log(this.state.users);
    return (
      <div>
        <LoginForm></LoginForm>

        <form>
          <input type="text" placeholder="email" value={this.state.email} onChange={e => 
               this.setState({email: e.target.value})} />
          <input type="text" placeholder="password" value={this.state.password} onChange={e => 
              this.setState({password: e.target.value})} />
          <input type="submit" onPress={this.onSubmit} />
        </form>
      </div>
    );
  }
}

如果您需要进一步的翻译或解释,请告诉我。

英文:

You should try to avoid use refs in react unless you have a good reason to use them (some things like animations need to be controlled imperatively).

The React way is to do things declaratively with state, so changing an input updates the associated state field, and then the onSubmit function takes the values from state. Something like this:

class AddUsers extends React.Component {
constructor() {
super();
this.state = { users: [], email: &#39;&#39;, password: &#39;&#39; };
this.onSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e) {
e.preventDefault();
const data = { email: this.state.email, password: this.state.password };
fetch(&quot;/admin-Add-Users&quot;, {
method: &quot;POST&quot;, // or &#39;PUT&#39;
headers: {
&quot;Content-Type&quot;: &quot;application/json&quot;
},
body: JSON.stringify(data)
})
.then(response =&gt; response.json())
.then(data =&gt; {
console.log(&quot;Success:&quot;, data);
})
.catch(error =&gt; {
console.error(&quot;Error:&quot;, error);
});
}
render() {
console.log(this.state.users);
return (
&lt;div&gt;
&lt;LoginForm&gt;&lt;/LoginForm&gt;
&lt;form&gt;
&lt;input type=&quot;text&quot; placeholder=&quot;email&quot; value={this.state.email} onChange={e =&gt; 
this.setState({email: e.target.value})} /&gt;
&lt;input type=&quot;text&quot; placeholder=&quot;password&quot; value={this.state.password} onChange={e =&gt; 
this.setState({password: e.target.value})} /&gt;
&lt;input type=&quot;submit&quot; onPress={this.onSubmit} /&gt;
&lt;/form&gt;
&lt;/div&gt;
);
}
}

huangapple
  • 本文由 发表于 2020年1月6日 22:09:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定