javascript - 未捕获的类型错误:无法设置未定义的属性“键”
问题描述
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 控制台错误:
未捕获的类型错误:无法设置未定义的属性“键”:
解决方案
您没有在任何地方声明 productKey 。
var productKey;
推荐阅读
- c# - 并行任务比单个任务花费更长的时间
- v4l2 - 如何使用 V4L2 请求检测 HDMI IN 连接/断开连接?
- c++ - 显示错误输出的简单数组排序程序
- javascript - 如何将脚本添加到脚本js
- java - 线程堆栈通过 MAT OQL 归档
- r - 在 ggplot 函数中传递列名时轴标签错误
- google-cloud-platform - 如何解密存储在谷歌云存储中的 .pgp 文件?
- hyperledger-fabric - 使用区块链追踪资产或物品
- android - 如果我不需要房间数据库类型转换器的时间和日期怎么办
- python - Kivy ScreenManager 给了我一个黑屏