【发布时间】: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