我是 Meteor 的新手,所以我一直在玩,现在我遇到了这个问题。
我正在使用 React Router 尝试显示基于 URL/(:userId) 的主题。如果 URL 中没有插入 userId,它应该显示当前用户的主题,如果没有当前用户,它应该显示默认主题。
它是随机工作的。有时我得到正确的主题,有时它在读取 themeColor 时抛出未定义,即使数据在那里。我可以通过 console.log 看到它总是获得正确的 Id,但 findOne 仍然会抛出未定义的错误。它特别发生在我更改 URL (/xyz) 并返回默认值 (/) 时。
我通过控制台验证,userId 是 themeColor 和 themeTextColor 的实际所有者。
我正在使用 React、React-router、自动发布。我删除了不安全的。
getMeteorData() {
var currentViewedPageId = this.props.params.userId? this.props.params.userId:(Meteor.userId()?Meteor.userId():false);
console.log(currentViewedPageId); //Allways right
console.log(Personalization.findOne({owner: currentViewedPageId}).themeColor); //Sometimes undefined, sometimes not
if(currentViewedPageId)
{
return {
currentUser: Meteor.user(),
themeColor: Personalization.findOne({owner: currentViewedPageId}).themeColor,
themeTextColor: Personalization.findOne({owner: currentViewedPageId}).themeTextColor
};
}
return {
currentUser: Meteor.user()
}
},
最佳答案
因为代码有时能正常工作。可能有一些与架构不匹配的测试数据。所以测试集合中的所有数据。
关于javascript - findOne 抛出未定义,即使数据在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34830214/