首页 > 解决方案 > 使用 Netsuite Rest API 通过电话号码查询客户

问题描述

我正在尝试创建对 NetSuite REST API 的请求,在我的特定用例中,最容易获得的信息来源是客户编号。

我有一个axios助手设置来发出请求。想知道是否有人可以帮助我......这就是我到目前为止所拥有的。

    await ns.get('customer?q=phone')
  .then((res) => {
     console.log(res)
     console.log('success')
  })
  .catch((err) => {
      console.log(err);
     console.log('error')
  })

现在不幸的是,这不起作用。有任何想法吗?

谢谢你的时间!

编辑:找到我的解决方案!

    await ns.get('customer?q=phone IS <Customer Number Here>')
  .then((res) => {
      console.log('success')
      let custID = res.data.items[0].id
      console.log(custID);
      await ns.get(`customer/${custID}`)
         .then((res) => {
            console.log(res);
         })
  })
  .catch((err) => {
      console.log(err);
     console.log('error')
  })

此代码返回一个非常大的对象。打开 response.data.items 给了我客户的 ID。然后用这个 id 提出一个新的请求给了我我需要的信息。

标签: javascriptapirestaxiosnetsuite

解决方案


第一个解决方案:删除await关键字。

 ns.get('customer?q=phone')
  .then((res) => {
     console.log(res)
     console.log('success')
  })
  .catch((err) => {
      console.log(err);
     console.log('error')
  })

否则,您必须定义async才能await像这样使用

var getData = () => new Promise(resolve => resolve("Your data"));

async function run(){
  try{
    var res = await getData();// ns.get('customer?q=phone')
     console.log(res);
     console.log('success');
  }catch(err) {
      console.log(err);
     console.log('error')
  }
}

run();


推荐阅读