【问题标题】:How to debug err: "Uncaught (in promise) Missing Argument Unit"如何调试错误:“未捕获(承诺)缺少参数单元”
【发布时间】:2016-03-27 07:31:20
【问题描述】:

我以前从未见过此错误。我写了一个丑陋的承诺链(如下),我怀疑是在扔它,但是当我注释掉该部分时,它甚至是在扔。没有其他任何承诺被摆弄。

假设这不是很明显,我该如何调试?

来自 chrome 控制台的错误(我从未将 undefined:1 视为来源):

>Uncaught (in promise) Missing argument unit       undefined:1

这是代码。两个特点:(1)当我注释掉这个块时,错误仍然发生,并且(2)在开头的两条console.log() 行中,第一个在抛出错误之前触发,第二个从不触发。 (1) 告诉我这不是问题,但 (2) 告诉我是问题。

这也是自上次提交以来我编辑的唯一代码块。根据console.log() 测试,其他编辑(所有次要的,没有异步)似乎工作正常。

ETA:在没有任何承诺的情况下完全重写本节后,我仍然收到错误消息。现在有点烦。

export const startListeningToAuth = function () {

    return function (dispatch, getState) {
        console.log("BEFORE LISTENER ATTACH");
        firebaseRef.onAuth(function (authData) {
                    console.log("AFTER LISTENER ATTACH");

                    if (authData) {
                        if ('uid' in authData) {

                            //set up profile listener

                            //does user's auth ID exist in our authID:experimentUID hash?
                            firebaseRef.child('path/to/hash').once('value')

                                    .then(function (snap) {
                                        //if user is new, confirm that new users' experiment IDs are not taken
                                        if (!snap.exists()) {
                                            firebaseRef.child('path/to/profiles').once('value', function (snapshot) {

                                                //if profile exists, create new experimentUID,
                                                // write record to firebase hash (to overwrite val we wrote above)
                                                // , save, and dispatch it
                                                if (snapshot.exists()) {
                                                    let newCookieUID = generateUniqueID();
                                                    Cookies.set('cookieUID', newCookieUID);

                                                    firebaseRef.child('path/to/hash').set(newCookieUID, function (err) {
                                                        dispatch(getCookieUID(newCookieUID));
                                                        return newCookieUID;
                                                    })
                                                }
                                            })
                                        } else {
                                            return snap.val()
                                        }
                                    })


                                    .then(function (snap) {

                                        //assign new ref to global var so we can turn it .off() in another function
                                        userProfileRef = new Firebase(firebaseURLs.users['renters']);

                                        //attach listener to cookieUID we came up w/
                                        var profileListener = userProfileRef.child(snap).on("value", function (snapshot) {

                                                    //if user already exists, load their profile into state
                                                    if (snapshot.exists()) {
                                                        dispatch({type: C.LOAD_USER_PROFILE, userProfile: snapshot.val()});
                                                    } else {

                                                        //if not, parse a new profile from their auth data
                                                        let newProfile = authPayloadToUserProfile(authData);

                                                        //write profile to firebase. no return value
                                                        createNewUserProfile(newProfile, 'renters', cookieUID);


                                                    }
                                                }
                                        )
                                    })
                                    .catch(function (err) {
                                        console.log(err)
                                    })


                        }


                    } else {
                        //if auth.on() fires null event, log user out in state
                        if (getState().userAuth.authStatus !== C.ANONYMOUS) { 
                            dispatch({type: C.LOGOUT});
                        }
                    }
                }
        );
    }
};

【问题讨论】:

  • Promises 有一个 .catch() 方法来处理异常,查看developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • “我写了一个丑陋的承诺链,我怀疑是在抛出它” 如果没有看到引发此错误的代码,将很难为您提供帮助。如果可能,请提供MCVE。还要记住,文本的屏幕截图是发布该文本的最不可重复使用的方式之一。添加实际文本并将其标记为>(块引用)。

标签: javascript reactjs firebase redux


【解决方案1】:

好的,我找到了造成这种情况的原因,但我仍然很想听听关于为什么会发生这种情况的想法。控制台错误非常神秘,我想留下一个很好的解释,以防其他人点击它(因为没有谷歌点击这个错误消息)

在一个 React 组件中,我有一个来自 react-experiments/planout.js 的函数,它将用户分配到 A/B 测试组。我需要来自 state 的 UID 来执行此操作,因此在我的 mapStateToProps(state) 函数中,除了将 ID 存储在组件的 props 中之外,我还创建了一个用于存储 ID 的全局变量:

//assign user to experiment group
var exp = new SampleExperiment({'expUserID': UID});

{...component code...}

var UID;
function mapStateToProps(state) {
    UID = ;
    return {
        listing: state.listings.listingData,
        UID: UID
    }
};

同样,不知道为什么,但删除了 var UID 并更改了我的实验分配代码。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2018-05-12
  • 2019-08-20
相关资源
最近更新 更多