AngularJS尝试与微风的SaveChanges时抛出异常 [英] AngularJS throws exception when trying to saveChanges with breeze

查看:182
本文介绍了AngularJS尝试与微风的SaveChanges时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来AngularJS和微风。我想保存更改并有一个问题。这里是我的code:

在控制器:

 函数的更新(){
        vm.loading = TRUE;
        返回datacontext.saveSettings()。然后(函数(){
            vm.loading = FALSE; //不会被调用
        })失败(功能(数据){
            vm.loading = FALSE; //不会被调用
        });
    }

在DataContext的:

 函数saveSettings(){
        如果(manager.hasChanges()){
            manager.saveChanges()如果有变化//这里打破
                。然后(saveSucceeded)
                .fail(saveFailed)
                .catch(saveFailed);
        }其他{
            日志(没什么保存);
            返回false;
        };
    }

该错误是在 angular.js 抛出,这是非常无益的类型错误:未定义的是不是一个函数我猜有一些简单的我缺少在这里,但无法弄清楚它是什么。

要注意的是它不正确的数据发送到SaveChanges方法在服务器上,但错误是收到服务器的响应之前抛出。在接收到响应后,它会引发另一个错误类型错误:无法读取的不确定,但它可能与事实我返回响应是无效的财产地图。我没有得到该部分还没有。

任何人任何人都可以帮助?我在这里丢失了。

更新

下面是我如何构造我的DataService和管理者:

  VAR服务名=HTTP://admin.localhost:33333 / API /微风/; //
变种DS =新breeze.DataService({
    服务名:服务名,
    hasServerMetadata:假的,
    useJsonp:真实,
    jsonResultsAdapter:jsonResultsAdapter
});
VAR经理=新breeze.EntityManager({DataService的:DS});
model.initialize(manager.metadataStore);


解决方案

两个问题:


  1. 的DataContext 方法不返回的承诺,以便调用者找不到什么挂然后失败呼吁。


  2. 您应该调用,而不是失败


1。返回一个承诺

saveSettings 方法并没有成功的情况下返回结果,所以必须失败。你的方法也必须在故障情况下返回一个承诺......否则也会失败。

虽然我在这里,因为有你的成功之间没有真正的区别和失败的情况下,你还不如移动 vm.loading 切换到最后子句。

试试这个:

 函数的更新(){
    vm.loading = TRUE;
    返回datacontext.saveSettings()
        。然后(函数(){
            // ..成功处理,如果您有任何
        })
        .catch(功能(数据){
            // ..处理失败,如果您有任何
        })
        。最后(funtion(){
            vm.loading = FALSE; //关闭它不管
        });
}

而现在的的DataContext ...注意两个收益语句返回的承诺。

 函数saveSettings(){
    如果(manager.hasChanges()){
        返回manager.saveChanges()
            。然后(saveSucceeded)
            .catch(saveFailed);
    }其他{
        日志(没什么保存);
        //你为什么失败的时候有什么救?
        //微风会很好地处理这没有你的帮助
        返回breeze.Q.reject(新的错误(没有什么保存'));
    };
}

2。使用的

我假定你已经配置微风使用角的 $ Q 的承诺(你应该使用breeze.angular的服务,并已注入了清风的地方)。

$ Q 不有一个失败方法!相对应的是。出于某种原因,你都连接到您的查询。你会立即得到的ReferenceError 例外,服务器来不及反应......虽然会推出该请求,你会得到来自服务器的回调太前。

尽量只:

 返回manager.saveChanges()
    。然后(saveSucceeded)
    .catch(saveFailed);

您看到很多例子微风调用失败。这些都是Q.js的方法; Q.js是一个另类的承诺图书馆 - 一个接微风/淘汰赛应用程序使用,它是依据角的 $ Q

两者Q.js和 $ Q 支持现在标准最后承诺的方法。我们在我们的例子中code慢慢迁移到这个标准。有很多老失败 / 最后 $ C $的Ç各地不同的场地。这需要时间。

很抱歉的混乱。

I'm new to AngularJS and Breeze. I'm trying to save changes and have a problem with that. Here's my code:

In controller:

function update() {
        vm.loading = true;
        return datacontext.saveSettings().then(function () {
            vm.loading = false; // never gets called
        }).fail(function (data) {
            vm.loading = false; // never gets called
        });
    }

In datacontext:

 function saveSettings() {
        if (manager.hasChanges()) {
            manager.saveChanges() // breaks here if there are changes
                .then(saveSucceeded)
                .fail(saveFailed)
                .catch(saveFailed);
        } else {
            log("Nothing to save");
            return false;
        };
    }

The error is thrown in angular.js and it's very unhelpful TypeError: undefined is not a function I guess there is something simple I'm missing here, but can't figure out what is it.

Want to note that it does send correct data to SaveChanges method on server, but the error is thrown before any response from the server received. After the response is received it throws another error TypeError: Cannot read property 'map' of undefined but it might be related to the fact the response I return is invalid. I haven't got to that part yet.

Can anyone anyone help with it? I'm lost here.

UPDATE

Here is how I construct my dataservice and manager:

var serviceName = "http://admin.localhost:33333/api/breeze/"; // 
var ds = new breeze.DataService({
    serviceName: serviceName,
    hasServerMetadata: false,
    useJsonp: true,
    jsonResultsAdapter: jsonResultsAdapter
});
var manager = new breeze.EntityManager({ dataService: ds });
model.initialize(manager.metadataStore);

解决方案

Two problems:

  1. Your datacontext method does not return a promise so the caller cannot find anything to hang the then or fail call on.

  2. You should be callingcatch, not fail

1. Return a promise

Your saveSettings method did not return a result in the success case so it must fail. Your method must also return a promise in the fail case ... or it will also fail.

And while I'm here, since there is no real difference between your success and fail case, you might as well move the vm.loading toggle to the finally clause.

Try this instead:

function update() {
    vm.loading = true;
    return datacontext.saveSettings()
        .then(function () {
            // .. success handling if you have any
        })
        .catch(function (data) {
            // .. fail handling if you have any
        })
        .finally(funtion() {
            vm.loading = false; // turn it off regardless
        });
}

And now the dataContext ... notice the two return statements return a promise.

function saveSettings() {
    if (manager.hasChanges()) {
        return manager.saveChanges() 
            .then(saveSucceeded)
            .catch(saveFailed);
    } else {
        log("Nothing to save");
        // WHY ARE YOU FAILING WHEN THERE IS NOTHING TO SAVE? 
        // Breeze will handle this gracefully without your help
        return breeze.Q.reject(new Error('Nothing to save'));
    };
}

2. Use catch

I assume you have configured Breeze to use Angular's $q for promises (you should be using the "breeze.angular" service and have injected "breeze" somewhere).

$q does not have a fail method! The equivalent is catch. For some reason you have both attached to your query. You'll get the ReferenceError exception immediately, before the server has a chance to respond ... although it will launch the request and you will get a callback from the server too.

Try just:

return manager.saveChanges() 
    .then(saveSucceeded)
    .catch(saveFailed);

You see many Breeze examples that call fail and fin. These are "Q.js" methods; "Q.js" is an alternative promise library - one used by Breeze/KnockOut apps and it was the basis for Angular's $q.

Both "Q.js" and $q support the now-standard catch and finally promise methods. We're slowly migrating our example code to this standard. There is a lot of old fail/finally code around in different venues. It will take time.

Sorry for the confusion.

这篇关于AngularJS尝试与微风的SaveChanges时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆