Javascript提取Api无法启动问题

时间:2019-12-14 19:21:41

标签: javascript api fetch

我有一个简单的代码,我正在尝试发送帖子,但是我的代码有问题。张贴不起作用,在幕后,我有一个简单的后端,并且可以100%使用。所以问题出在上面的代码。 请帮助我,范围可能有问题吗?

Api = function() {
  this.header = new Headers({
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  })
};

Api.prototype.buildUrl = function(id) {
  return "http://localhost:3000/db/shop_name/" + id;
};

Api.prototype.post = function(id, data) {

  const urlPost = api.buildUrl(id.value);
  return fetch(urlPost, {
      method: "post",
      body: JSON.stringify(data),
      headers: this.header

   .then(res => res.json()).then(json => console.log(JSON.stringify(json)))
};

Api.prototype.get = function(id) {
  //const urlGet = api.buildUrl(id);
  return fetch(id, {
      method: "GET",
    })
    .then(resp => {
      alert(resp.json());

      return resp.json();
    })
};
Api.prototype.getAlll = function() {
  return fetch(url, {
      method: "GET"
    })
    .then(resp => {
      alert(resp.json());
      return resp.json()
    })
};

Api.prototype.update = function(id, data) {
  const url = api.buildUrl(id);
  return fetch(url, {
      method: "PUT",
      body: JSON.stringify(data)
    })
    .then(resp => {
      return resp.json()
        .catch(error => {
          let notFound = "The server can not find requested resource";
          document.getElementById("stars").innerHTML = notFound + error.status;
        })
    })
};


Api.prototype.addProduct = function(id, data) {
  return this.post(id, data);
};

Api.prototype.deleteProduct = function(id) {
  return this.delete(id);
};

Api.prototype.updateProduct = function(id, data) {
  return this.update(id, data);
};

Api.prototype.getProduct = function(id) {
  return this.get(id);
};
Api.prototype.getAllProducts = function() {
  return this.getAlll;
};

const Main = function() {
  this.id = document.getElementById("id");
  this.addCount = document.getElementById("count");
  this.addName = document.getElementById("name");
  this.addPrice = document.getElementById("price");
};


Main.prototype.add = function() {
  // const ido = this.id.value;
  const data = {
    "price": this.addPrice.value,
    "name": this.addName.value,
    "count": this.addCount.value,
  };
  //  let id = api.buildUrl(this.id.value);
  api.addProduct(this.id, data);
};


Main.prototype.update = function() {
  const data = {
    "price": this.price,
    "name": this.name,
    "count": this.count,
  };
  api.updateProduct(id, data);
};

Main.prototype.delete = function() {
  let id = api.buildUrl(this.id);
  api.deleteProduct(id);

};


Main.prototype.get = function() {
  let id = api.buildUrl(this.id.value);
  api.getProduct(id);

};

Main.prototype.getAll = function() {
  api.getAllProducts();

};

const api = new Api();
const main = new Main();


let addButton = document.getElementById('postBtn');
addButton.addEventListener('click', function() {
  main.add();
});

/*
addButton.addEventListener("click",main.add.bind(main));
*/


let updateButton = document.getElementById("updateBtn");
updateButton.addEventListener("click", function() {
  main.update();
});


let deleteButton = document.getElementById("deleteBtn");
deleteButton.addEventListener("click", function() {
  main.delete();
});

let getButton = document.getElementById("getBtn");
getButton.addEventListener("click", function() {
  main.get();
});


let getAllButton = document.getElementById("getAllBtn");
getAllButton.addEventListener("click", function() {
  let tst = main.getAll();
  console.log(tst);

});

1 个答案:

答案 0 :(得分:1)

我创建了一个最小的工作示例,该示例仅处理您的post方法,我认为问题在于您为发布请求“构建” URL的方式:

Api.prototype.post = function(id, data) {
  // WRONG!
  // const urlPost = api.buildUrl(id)

  // CORRECT!
  const urlPost = this.buildUrl(id)

  return fetch(...)
}

看看这个示例,它的结构与您的代码类似:

const Api = function() {
  this.headers = new Headers({
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  })
}
Api.prototype.buildUrl = function(id) {
  return `https://jsonplaceholder.typicode.com/posts`
};
Api.prototype.post = function(id, data) {
  const urlPost = this.buildUrl()

  return fetch(urlPost, {
    method: 'POST',
    body: JSON.stringify({
      title: 'foo',
      body: 'bar',
      userId: 1
    }),
    headers: this.headers,
  })
  .then(response => response.json())
};


function main() {
  const API = new Api()
  const submitBtn = document.getElementById(`submitBtn`)
  
  submitBtn.addEventListener(`click`, function() {
    API.post()
      .then(result => {
        console.log(`data was submitted`, Date.now())
        console.log(`result`, result)
      })
      .catch(error => {
        console.error(`handle your errors!`)
      })
  })
}

main()
<button id="submitBtn" type="button">Click to submit data</button>

希望这会有所帮助。


编辑

您必须在其他所有地方解决此问题,而不仅仅是post方法。

首先,您无法在api方法内访问Api.prototype.post,因为您使用的是const,所以它不在范围内,因此在使用this时正在尝试在您的对象实例上调用方法:

const Api = function() { ... }
Api.prototype.buildUrl = function() { ... }
Api.prototype.post = function(id, data) {
  const urlPost = this.buildUrl()
  // ...  
}

const api = new Api()
api.post(...)

第二,始终处理您的错误,并确保您catch承诺失败。您很可能无法调试此问题,因为您错过了可能为您提供正在发生情况的线索的错误。

Api.prototype.post = function(id, data) {
  const urlPost = this.buildUrl()

  return fetch(urlPost, {...})
    .then(...)
    .catch(error => { console.log(`error`, error.message) }
}

再次,您遇到范围问题-apiApi实例的方法中没有Main

Api.prototype.post = function(...) {
  api.buildUrl(...) // NOT IN SCOPE!
}
Main.prototype.getAll = function() {
  api.getAllProducts() // ALSO NOT IN SCOPE
}

执行此操作:

const Main = function(api) {
  this.api = api
  // ...
}
Main.prototype.add = function() {
  const data = {...}
  return this.api.addProduct(this.id, data)
}

const api = new Api()
const main = new Main(api) // PASS `api` TO A CONSTRUCTOR
const btn = document.getElementById(`button`)

btn.addEventListener(`click`, function() {
  main.add()
    .then(...)
    .catch(...)
})

返回并修复ApiMain内的所有方法!

这是一个可行的示例:

const Api = function() {
  this.headers = new Headers({
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  })
}
Api.prototype.buildUrl = function(id) {
  return `https://jsonplaceholder.typicode.com/posts`
};
Api.prototype.post = function(id, data) {
  const urlPost = this.buildUrl()

  return fetch(urlPost, {
    method: 'POST',
    body: JSON.stringify({
      title: 'foo',
      body: 'bar',
      userId: 1
    }),
    headers: this.headers,
  })
  .then(response => response.json())
};
Api.prototype.addProduct = function(id, data) {
  return this.post(id, data)
}

function Main(api) {
  this.api = api
  this.id = 1
}
Main.prototype.add = function() {
  const data = {
    "price": 1,
    "name": "Bob",
    "count": 20
  }
  return this.api.addProduct(this.id, data)
}

const api = new Api()
const main = new Main(api)
const submitBtn = document.getElementById(`submitBtn`)

submitBtn.addEventListener(`click`, function() {
  main.add()
    .then(result => {
      console.log(`data was submitted`, Date.now())
      console.log(`result`, result)
    })
    .catch(error => {
      console.log(`handle your errors!`)
    })
})
<button id="submitBtn" type="button">Click me!</button>