【发布时间】:2019-03-14 10:03:21
【问题描述】:
Chrome 抛出 Uncaught TypeError: Cannot set property 'key' of undefined 错误,但我真的不知道代码有什么问题。我试过 console.log(item) 并且它是未定义的。但是使用 lodash 和 clone,我不确定如何设置值。很抱歉提出这样一个菜鸟问题,但如果有人能向我解释发生了什么,那就太好了,我一定会花很多时间从中学习!
下面是我的代码...
class Actions {
initSession() {
return (dispatch) => {
Firebase.auth().onAuthStateChanged(function(result) {
var profile = null;
if (result) {
profile = {
id: result.uid,
name: result.providerData[0].displayName,
avatar: result.providerData[0].photoURL
}
}
dispatch(profile);
});
}
}
login() {
return (dispatch) => {
var provider = new Firebase.auth.FacebookAuthProvider();
Firebase.auth().signInWithPopup(provider).then(function(result) {
var user = result.user;
var profile = {
id: user.uid,
name: user.providerData[0].displayName,
avatar: user.providerData[0].photoURL
}
Firebase.database().ref('/users/'+user.uid).set(profile);
dispatch(profile);
}).catch(function(error) {
console.log('Failed!', error);
});
}
}
logout() {
return(dispatch) => {
Firebase.auth().signOut().then(function() {
// Sign-out successful.
dispatch(null);
}, function(error) {
// An error happened.
console.log(error);
});
}
}
getComments(productId) {
return (dispatch) => {
var commentRef = Firebase.database().ref('comments/'+productId);
commentRef.on('value', function(snapshot) {
var commentsValue = snapshot.val();
var comments = _(commentsValue).keys().map((commentKey) => {
var item = _.clone(commentsValue[commentKey]);
item.key = commentKey;
return item;
})
.value();
dispatch(comments);
});
}
}
getProducts() {
return(dispatch) => {
Firebase.database().ref('products').on('value', function(snapshot) {
var productsValue = snapshot.val();
var products = _(productsValue).keys().map((productKey) => {
var item = _.clone(productsValue[productKey]);
item.key = productKey;
return item;
})
.value();
console.log(item);
dispatch(products);
});
}
}
getProducts() {
return(dispatch) => {
Firebase.database().ref('products').on('value', function(snapshot) {
var productsValue = snapshot.val();
var products = _(productsValue).keys().map((productKey) => {
var item = _.clone(productsValue[productKey]);
item.key = productKey;
return item;
})
.value();
dispatch(products);
});
}
}
addProduct(product) {
return (dispatch) => {
Firebase.database().ref('products').push(product);
}
}
addVote(productId, userId) {
return (dispatch) => {
var voteRef = Firebase.database().ref('votes/'+productId+'/'+userId);
var upvoteRef = Firebase.database().ref('products/'+productId+'/upvote');
voteRef.on('value', function(snapshot) {
if(snapshot.val() == null) {
voteRef.set(true);
var vote = 0;
upvoteRef.on('value', function(snapshot) {
vote = snapshot.val();
});
upvoteRef.set(vote+1);
}
});
}
}
addComment(productId, comment) {
return (dispatch) => {
Firebase.database().ref('comments/'+productId).push(comment);
}
}
getComments(productId) {
return (dispatch) => {
var commentRef = Firebase.database().ref('comments/'+productId);
commentRef.on('value', function(snapshot) {
var commentsValue = snapshot.val();
var comments = _(commentsValue).keys().map((commentKey) => {
var item = _.clone(commentsValue[commentKey]);
item.key = commentKey;
return item;
})
.value();
dispatch(comments);
});
}
}
}
export default alt.createActions(Actions);
这是我遇到的错误......
Chrome 控制台错误:
未捕获的类型错误:无法设置未定义的属性“键”:
【问题讨论】:
-
请分享快照示例
-
我已经编辑了帖子!
-
添加到productsValue的JSON数据,否则帮不上忙
-
请看这个!我认为它没有正确掌握子路径,因此将项目显示为未定义。 dropbox.com/s/fla60n4zlsw5106/codehunt-demo-export.json?dl=0
标签: javascript