我有路由发布请求的问题我需要建立寄存器表单并从表单到mongodb的后输入我在服务器端制作了路由器和发布路由它工作正常(当我使用邮递员时)
//表单是必需的模型
router.route('/').post(function(req,res,next){
res.send(req.body)
form.create(
{"first_name": req.body.first_name,
"last_name": req.body.last_name
})
.then(function(data){
res.send(data);
console.log(data);
}).catch(function(err){console.log(err)});
});Run Code Online (Sandbox Code Playgroud)
但我需要从客户端解决它,而不是邮递员.在这里,我迷失了.我可以这样做但是当我添加onSubmit动作时它不起作用.而且我需要使用新函数来触发另一个东西而不重定向到另一个页面.如何将this.refs.first_name.value传递给body,以便我可以使用fetch函数?以下反应成分
添加了此JavaScript/JSON代码段
export default class Form extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event){
event.preventDefault();
console.log(this.refs.first_name.value);
fetch('/', {
method: 'post',
body: {
"first_name": this.refs.first_name.value
}
});
};
render () {
return (
<div id="signup">
<form onSubmit={this.handleSubmit}>
<input ref="first_name" placeholder="First Name" type="text" name="first_name"/><br />
<input placeholder="Last Name" type="text" name="last_name"/><br />
<button type="Submit">Start</button>
</form>
?
</div>
?
)
}
}Run Code Online (Sandbox Code Playgroud)
我已经有一个用MERN堆栈编写的应用程序,其中包含koa服务器准备好的构建版本。我通过node server.js命令运行以启动整个应用程序的主节点文件如下所示。
在每个教程中,我都看到我需要functions.https.request在编码开始时添加等(或至少假设这样做)。我如何像在heroku上一样在Firebase上托管我的应用程序-整个服务器端?
node.js firebase koa firebase-hosting google-cloud-functions
我已经用webpack-middleware建立了反应全栈环境.我的代码中有一些es6语法但是在没有构造函数或命名箭头函数的情况下我得到错误.例如,我想使用semantic-ui作为反应排序表:https: //react.semantic-ui.com/collections/table#table-example-sortable 在编译时我收到此错误: 在此处输入图像描述
我认为这是因为我在下面附加了错误的webpack设置.
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './client/index.js',
output: {
path: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: 'client/index.html'
})
]
};
Run Code Online (Sandbox Code Playgroud)
.babelrc
{
"presets": ["env", "react", "es2015"]
}
Run Code Online (Sandbox Code Playgroud) reactjs ×2
ecmascript-6 ×1
fetch ×1
firebase ×1
javascript ×1
koa ×1
mongodb ×1
node.js ×1
rest ×1
webpack ×1