向服务器发出 POST 请求以插入记录并在客户端检索生成的 ID 的方法
Way to make a POST request to server to INSERT a record and retrieve the generated ID in the Client
我正在开发一个包含下表的应用程序:客户 |运输。 Client_Id 是 Transports 的外键。
我必须为传输制作一个创建向导,在第一步中我可能需要创建一个客户端并在下一步中通过 React 状态传递 client_id。我想要实现的是 POST 我的服务器创建客户端并在同一个请求中检索生成的 client_id.
那是我的后端代码:
exports.createClient = function(req,res,next){
const text = 'INSERT INTO public.client (name, "isCompany", mobile, phone, email, tax_code, addr) VALUES (, , , , , , ) RETURNING id'
const values = [req.body.name,req.body.isCompany,req.body.mobile,req.body.phone,req.body.email,req.body.tax_code,req.body.addr];
DBclient.query(text,values,function(err,result){
if (err){
console.log(err)
res.status(400).send(err);
}
res.sendStatus(200).send(result.rows[0].id);
});
};
我在这里尝试检索响应:
var formData = new FormData();
formData.append('name', this.state.client_name);
formData.append('isCompany', this.state.client_isCompany);
formData.append('tax_code', this.state.client_tax_code);
formData.append('addr', this.state.client_addr);
formData.append('phone', this.state.client_phone);
formData.append('mobile', this.state.client_mobile);
formData.append('email', this.state.client_email);
await fetch(clienturl, {
method: 'POST',
body: formData
}).then(data => this.setState({client_id:data}));
这是我之后的状态:
console log output for state
如果有人能对此提供帮助,我将不胜感激。
在多次尝试使用 postman 和 console.log() 调试之后,我意识到问题出在服务器端,而我没有设置响应的主体。在邮递员上,我看到服务器只响应状态代码(200 OK)。使这项工作有效的更新是:
DBclient.query(text,values,function(err,result){
if (err){
console.log(err)
res.status(400).send(err);
}
res.status(200).send({client_id : result.rows[0].id});
});
我正在开发一个包含下表的应用程序:客户 |运输。 Client_Id 是 Transports 的外键。 我必须为传输制作一个创建向导,在第一步中我可能需要创建一个客户端并在下一步中通过 React 状态传递 client_id。我想要实现的是 POST 我的服务器创建客户端并在同一个请求中检索生成的 client_id.
那是我的后端代码:
exports.createClient = function(req,res,next){
const text = 'INSERT INTO public.client (name, "isCompany", mobile, phone, email, tax_code, addr) VALUES (, , , , , , ) RETURNING id'
const values = [req.body.name,req.body.isCompany,req.body.mobile,req.body.phone,req.body.email,req.body.tax_code,req.body.addr];
DBclient.query(text,values,function(err,result){
if (err){
console.log(err)
res.status(400).send(err);
}
res.sendStatus(200).send(result.rows[0].id);
});
};
我在这里尝试检索响应:
var formData = new FormData();
formData.append('name', this.state.client_name);
formData.append('isCompany', this.state.client_isCompany);
formData.append('tax_code', this.state.client_tax_code);
formData.append('addr', this.state.client_addr);
formData.append('phone', this.state.client_phone);
formData.append('mobile', this.state.client_mobile);
formData.append('email', this.state.client_email);
await fetch(clienturl, {
method: 'POST',
body: formData
}).then(data => this.setState({client_id:data}));
这是我之后的状态: console log output for state
如果有人能对此提供帮助,我将不胜感激。
在多次尝试使用 postman 和 console.log() 调试之后,我意识到问题出在服务器端,而我没有设置响应的主体。在邮递员上,我看到服务器只响应状态代码(200 OK)。使这项工作有效的更新是:
DBclient.query(text,values,function(err,result){
if (err){
console.log(err)
res.status(400).send(err);
}
res.status(200).send({client_id : result.rows[0].id});
});