Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name:grocery is not defined in grocery-list-view-model.js line 37 #24

Closed
NelsonRock opened this issue Oct 16, 2015 · 1 comment
Closed

Comments

@NelsonRock
Copy link

var config = require("../../shared/config");
var fetchModule = require("fetch");
var observableArrayModule = require("data/observable-array");

function GroceryListViewModel(items) {
var viewModel = new observableArrayModule.ObservableArray(items);

viewModel.load = function() {
  return fetch(config.apiUrl + "Groceries", {
    headers:{
      "Authorization" : "Bearer" + config.token
     }
  })
  .then(handleErrors)
  .then(function(response) {
    return response.json();
  })
  .then(function(data){
    data.Result.forEach(function(grocery){
      viewModel.push({
        name: grocery.Name,
        id: grocery.Id
      });
    });
  });
};

viewModel.empty = function () {
  while (viewModel.length) {
    viewModel.pop();
  }
};

viewModel.add = function () {
  return fetchModule.fetch(config.apiUrl + "Groceries", {
    method: "POST",
    body: JSON.stringify({
        Name:grocery  //error reference
    }),
    headers: {
      "Authorization" : "Bearer" + config.token,
      "Content-Type" : "application/json"
    }
  })
  .then(handleErrors)
  .then(function(response){
    return response.json();
  })
  .then(function(data){
    viewModel.push({
      name:grocery,
      id:data.Result.Id
    });
  });
};

return viewModel;

}

function handleErrors(response) {
if (!response.ok) {
console.log(JSON.stringify(response));
throw Error(response.statusText);
}
return response;
}

module.exports = GroceryListViewModel;

@NelsonRock
Copy link
Author

Hi, don't work a lot , that what's happen if you work on three projects in same time ;) a simple passgin variable to function in add is simplest even for beginners like me.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant