admin管理员组

文章数量:1333476

I want to store JSON data in a database using Axios in node js. This is my node js code

const axios = require('axios');

const url = 'http://api/api/master_ab_store';

axios.post(url, {
    "data":[{"external_id":"-000001","code":"- ","name":"INVESTINDO","Phone":"031-5482334","Address":"Jalan Kelapa Puyuh","status_ab":"A"}]
})
.then(function(res){
    console.log(res);
})
.catch(function(err){
    console.log(err);
})

But i got an error like this

C:\cs_test>node index.js
Error: getaddrinfo ENOTFOUND 3128
    at GetAddrInfoReqWrap.onlookup [as onplete] (dns.js:67:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '3128',
  config: {
    url: 'http://api/api/master_ab_store',
    method: 'post',
    data: '{"data":[{"external_id":"-000001","KodeAB":"- ","NamaAB":"SUMBER ARTHA INVESTINDO","Phone":"031-5482041-44","Fax":"031-5482048","Address":"Kedungdoro No. 60 Lantai 7;Jawa Timur;Surabaya;60251","status_ab":"A"}]}',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/json;charset=utf-8',
      'User-Agent': 'axios/0.21.1',
      'Content-Length': 211,
      host: 'api'
    },

In postman, it's running well

How to fix this issue? help me because I'm newbie in node js

I want to store JSON data in a database using Axios in node js. This is my node js code

const axios = require('axios');

const url = 'http://api/api/master_ab_store';

axios.post(url, {
    "data":[{"external_id":"-000001","code":"- ","name":"INVESTINDO","Phone":"031-5482334","Address":"Jalan Kelapa Puyuh","status_ab":"A"}]
})
.then(function(res){
    console.log(res);
})
.catch(function(err){
    console.log(err);
})

But i got an error like this

C:\cs_test>node index.js
Error: getaddrinfo ENOTFOUND 3128
    at GetAddrInfoReqWrap.onlookup [as onplete] (dns.js:67:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '3128',
  config: {
    url: 'http://api/api/master_ab_store',
    method: 'post',
    data: '{"data":[{"external_id":"-000001","KodeAB":"- ","NamaAB":"SUMBER ARTHA INVESTINDO","Phone":"031-5482041-44","Fax":"031-5482048","Address":"Kedungdoro No. 60 Lantai 7;Jawa Timur;Surabaya;60251","status_ab":"A"}]}',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/json;charset=utf-8',
      'User-Agent': 'axios/0.21.1',
      'Content-Length': 211,
      host: 'api'
    },

In postman, it's running well

How to fix this issue? help me because I'm newbie in node js

Share Improve this question edited Jun 10, 2021 at 7:56 Rahmat Effendi asked Jun 10, 2021 at 4:19 Rahmat EffendiRahmat Effendi 3433 gold badges11 silver badges30 bronze badges 10
  • 1 Make sure the URL http://api/api/master_ab_store is correct. ENOTFOUND error means the domain that you are trying to reach is unavailable or wrong – Đăng Khoa Đinh Commented Jun 10, 2021 at 4:47
  • Are you serving the api on your local machine? If so I would expect the url to be somthing like http://localhost:8080/api/api/master_ab_store or http://127.0.0.1:5001/api... – Mark Davich Commented Jun 10, 2021 at 4:55
  • @ĐăngKhoaĐinh when I test it on the postman, It's running well – Rahmat Effendi Commented Jun 10, 2021 at 7:15
  • @MarkDavich yes, the API on my local server. I make a dummy domain, I'm setting it on host file and vhost file xampp – Rahmat Effendi Commented Jun 10, 2021 at 7:17
  • @Rahmat Drrendi, Side note, what database are you using? Does your post data need to be a string in the manner that JSON.stringify(pojo) produces in javascript? Are you using a node backend? or something else? Did you try putting a forward slash at the end of your url in the node frontend? like url = ...master_ab_store/ – Mark Davich Commented Jun 10, 2021 at 7:31
 |  Show 5 more ments

2 Answers 2

Reset to default 3

I solved my issue. This is because the server has a proxy. So when I remove it, it's running. Thanks to @Mark Davich and @ĐăngKhoaĐinh for helping me

Make sure your url (baseURL) is correct. I use the free version of postman to check my api calls. Is your data object supposed to be an array? If not your endpoint my not know what to do with it

I don't use Axios or promises the way you do, so here is an alternative version of what you are trying to do. Maybe you it will help. The async/await replace the .then/.catch

import Axios from "axios";

axios = Axios.create({
  baseURL: 'http://api/api/master_ab_store',
  timeout: 10000,
  withCredentials: true
});

let data = {
  external_id: "-000001",
  code : "- ",
  name: "INVESTINDO",
  Phone: "031-5482334",
  Address: "Jalan Kelapa Puyuh",
  status_ab: "A"
}

async getData(data) {
  try {
    const res = await axios.post(url, data);
    console.log(res.data); // .then
  } catch (error) {
    console.log(err); // .catch
  }
}

本文标签: javascriptError getaddrinfo ENOTFOUND 3128 when post data using Axios in node jsStack Overflow