0

I am currently in a situation where I need to get data out of a Promise object in order to update my view via Vue. However I cannot seem to be able to get the data out of this promise object? Does anyone know how I can do it in order to access the data from the JSONresponse.

Please note I have made a POST request in order to create a new 'module' and now I need those values passed back in order to update my view.

Pease find below:

            async createModule() {
              let response = await fetch('create_module', {
                  method: 'POST', // Method itself
                  headers: {
                      "contentType": "application/json",
                      "X-CSRFToken": document.querySelector("[name=csrfmiddlewaretoken]").value,
                  },
                  body: JSON.stringify({
                      code: document.getElementById('code').value,
                      module_name: document.getElementById('module_name').value,
                      max_students: document.getElementById('max_students').value,
                      start_date: document.getElementById('start_date').value,
                  }) // We send data in JSON format
              });
              if (response.ok) {
                  // module was created
                  //this.modules.push(data);
                  console.log(response.json());  // HERE IS WHERE I AM TRYING TO ACCESS ALL THE VALUES FROM THE RESPONSE

              } else {
                  alert("failed to create module");
              }
          }

My API:

def create_module(request):
    """API for creating a module"""
    if request.method == "POST":
        PUT = json.loads(request.body)
        module = Module(module_name=PUT['module_name'], code=PUT['code'], max_students=PUT['max_students'], start_date=PUT['start_date'])
        module.save()
        return JsonResponse({'module_name': PUT['module_name'], 'code': PUT['code'], 'max_students': PUT['max_students'], 'start_date': PUT['start_date']})

    return HttpResponseBadRequest("Invalid method")

Thank you for your time.

Notorious
  • 41
  • 1
  • 7

0 Answers0