简体   繁体   中英

Why is my angular service 'then' method returning a resolved promise intead of the value?

I am not understanding why "result" is a resolved promise instead of the actual value here:

searchService.getLink(vm.queryObject).then(function (result) {
    console.log(result);
});

The code for getLink() is as follows:

function getLink(query) {
    return $resource(uriService.searchLink).save(query).$promise;
}

I'm doing something very similar elsewhere and I get the value in the then() callback instead. What am I missing?

Update I was mistaken, "result" is a Resource object that has a promise on it. But the question remains, why isn't the value of the promise being passed to the callback?

I figured out it was because of how I was returning data from the server. I just wanted to return an id from the server so I was trying this:

[HttpPost]
[Route("search/link")]
public int Link(SearchCriteria searchCriteria)
{
    return _searchService.Save(searchCriteria);
}

But $resource must require an object to be returned from the server, so I modified my return type to be an object and then the value started appearing as a property on that object.

[HttpPost]
[Route("search/link")]
public GenericModel<int> Link(SearchCriteria searchCriteria)
{
    return new GenericModel<int> { Data = _searchService.Save(searchCriteria) };
}

GenericModel<T> is just a class I made to wrap values for this purpose.

public class GenericModel<T>
{
    public T Data { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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