简体   繁体   English

另一个函数中的函数不会在 JS 中执行

[英]function inside another function doesn't execute in JS

In my current project, I have a function which calls an angular custom service and assign the data to a variable在我当前的项目中,我有一个函数调用 angular 自定义服务并将数据分配给变量

var vm = this;
vm.itemname = '';

function getItems(){
  var result = myservice.getFromLibrary();
  result.then(function(data){
     vm.dataSet = data;
  });
}

I have another function to save the data and after it get saved the getItem() function should be called, as follows我有另一个函数来保存数据,保存后应该调用 getItem() 函数,如下所示

vm.addItem = function(){
    var newItem = {"title": vm.itemname};
    myservice.setToLibrary(newItem);
    getItems();
};

but when the vm.addItem called, it doesn't retrieve the items in getItems function but it gives me an undefined output但是当 vm.addItem 调用时,它不会检索 getItems 函数中的项目,但它给了我一个未定义的输出

Any help will be highly appreciated to fix this issue任何帮助将不胜感激解决此问题

First of all, I you call the function name wrong, getItem should be getItems .首先,我你调用的函数名错了, getItem应该是getItems

If it is a typo, call the getItems method after the promise is returned from the service.如果是拼写错误,请在服务返回承诺后调用 getItems 方法。

You can even add the error function from the callback.您甚至可以从回调中添加错误函数。

When you use this promise(then and error) , the next lines of code executes only when the response comes from the service.当您使用此promise(then and error) ,仅当响应来自服务时才会执行下一行代码。

vm.addItem = function(){
    var newItem = {"title": vm.itemname};
    myservice.setToLibrary(newItem)
    .then(function(response){
        getItem();
    }
    function error(response) 
    {
     console.log('error')
    });
};

函数名称是getItems()但是当您从addItem调用它时,您正在调用一个未定义的函数getItem() - 您尝试调用的函数名称中缺少s

If I'm assuming right you are adding an item through service call myservice.setToLibrary(newItem);如果我假设正确,您正在通过服务调用myservice.setToLibrary(newItem);添加一个项目myservice.setToLibrary(newItem); and after that reading the item present in DB through getItems();然后通过getItems();读取 DB 中存在的项目getItems(); which is again a service call ie $http POST or GET .这又是一个服务调用,即$http POST or GET

Reason behind the 'undefined' output “未定义”输出背后的原因

As you can see in your vm.addItem() function you are doing two $http calls to add and read.As $http is asynchronous in nature,your getItems() is executed before even your myservice.setToLibrary(newItem) is completely executed.正如您在vm.addItem()函数中看到的,您正在执行两个$http调用来添加和读取。由于$http本质上是异步的,您的getItems()甚至在您的myservice.setToLibrary(newItem)完全执行之前就已执行.

Due to this you are getting an undefined output.因此,您将获得未定义的输出。

Solution approach解决方法

Make some adjustments in your vm.addItem() function like belowvm.addItem()函数中进行一些调整,如下所示

vm.addItem = function(){
    var newItem = {"title": vm.itemname};
    myservice.setToLibrary(newItem).success(function(data){
        getItems();
    });

This approach will get you desired result as we made our getItems() call after the success of our setToLibrary() call.当我们在setToLibrary()调用成功后进行getItems()调用时,这种方法将获得您想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM