Reputation: 225
I have nodeJs back end where i have created this api
app.post("/addInvoice", (req, res) => {
let invoice = req.body;
var sql =
"INSERT INTO invoice (address, email, billnumber, price, status) VALUES(?, ?, ?, ?, 'sent');";
mysqlConnection.query(
sql,
[
invoice.address,
invoice.customeremail,
invoice.billnumber,
invoice.totalprice,
],
(err, rows, fields) => {
if (!err) {
res.send(rows);
} else {
res.send(err);
}
}
);
});
when I call this API using post man it works perfectly.
but now i need to call it on buttonclick from my react-native app.
axios
.post("http://localhost:3000/addInvoice", {
address: address,
customeremail: email,
billnumber: invoicenumber,
totalprice: totalPrice,
})
.then((response) => {
alert(response);
});
I nothing happens when I send this and the alert(responce) returns nothing.
what could be the problem?
Upvotes: 1
Views: 228
Reputation: 545
Your Emulator (or Phone) is a device on it's own that is not running on the same IP(localhost or 127.0.0.1) as your web browser, postman or your server.
In order to make request to your server from your emulator (or phone) you need to access your server via your computer IP Address: On windows get your IP Address by running ipconfig on the command prompt, or maybe here: system preferences > Network > Advanced > TCP/IP > IPv4 Address.
Instead of http://localhost:port you should use http://your_ip_address:port
Upvotes: 2