如何使用“Object.keys”函数在vue.js中列出firebase子节点的长度时防止出现错误?(How to prevent an error when listing the length of a firebase child node in vue.js using an “Object.keys” function?)
想象一下Vue.js应用程序,其中firebase中的数据结构将发展为如下形式:
item: { name: itemName, childOne: { subChildA: true, subChildB: true }, childTwo: { subChildA: true, subChildB: true } }
现在我有一个组件,我需要列出子节点的长度(这些子节点只会在稍后添加)。 我的第一个想法是简单地做到这一点,但这种策略是行不通的:
<tbody> <tr v-for="item in items" > <td> {{item.childOne.length - item.childTwo.length}} </td> </tr> </tbody>
所以或者我得到这样的长度:
<tbody> <tr v-for="item in items" > <td> {{Object.keys(item.childOne).length - Object.keys(item.childTwo).length}} </td> </tr> </tbody>
正如我所说的,我需要在创建项目的子项之前渲染该组件......这些子项节点后来通过Google Cloud Function有条件创建,所以我不可避免地从这个简单的结构开始:
item: { name: itemName, }
...但在这个阶段,我收到一条错误消息:
TypeError:无法将未定义或null转换为Function.keys()处的对象
我在想,如果我使用第一种策略,vue.js足够聪明,可以避免此错误呈现组件,并且如果没有孩子,则不显示任何内容,但它无法获得长度 。
第二种策略在我添加孩子时非常有效,但它无法处理数据的初始缺失,甚至无法呈现视图 。
无论如何,当孩子节点不在那里时,使用第二种策略来防止这种错误?
Imagine a Vue.js app where the data structure in firebase will develop into something like this:
item: { name: itemName, childOne: { subChildA: true, subChildB: true }, childTwo: { subChildA: true, subChildB: true } }
Now I have a component where I need to list the length of the child nodes (these child nodes will only be added later). My first thought was to simply do this, but this strategy doesn't work:
<tbody> <tr v-for="item in items" > <td> {{item.childOne.length - item.childTwo.length}} </td> </tr> </tbody>
So alternatively I got the length like this:
<tbody> <tr v-for="item in items" > <td> {{Object.keys(item.childOne).length - Object.keys(item.childTwo).length}} </td> </tr> </tbody>
As I said I need to render that component before the item's children are created... These child nodes are later conditionally created through a Google Cloud Function so I inevitably start with this simple structure:
item: { name: itemName, }
...but at this stage I get an error message:
TypeError: Cannot convert undefined or null to object at Function.keys ()
I am thinking that if I use the first strategy, vue.js is smart enough to avoid this error renders the component, and displays nothing if there are no children, but it can't get the lengths.
The second strategy works perfectly when I add the children but it can't deal with the initial absence of data and doesn't even render the view.
Is there anyway to prevent this error using the second strategy when the child nodes are not there yet?
满意答案
您可以使用双管道运算符为
item.childOne
或item.childTwo
实例提供默认的空对象。改变这个:
{{Object.keys(item.childOne).length - Object.keys(item.childTwo).length}}
为此:
{{Object.keys(item.childOne || {}).length - Object.keys(item.childTwo || {}).length}}
小规模示例:
var test = {}; console.log(Object.keys(test.childOne || {}).length);
但是,您应该留意Vue无法检测到属性添加或删除。 根据文档 :
由于Vue在实例初始化期间执行getter / setter转换过程,因此数据对象中必须存在一个属性,以便Vue将其转换并使其处于被动状态。
根据您的具体用例,您可能需要在初始化过程中定义
childOne
或将其转换为数组,以便您可以利用Vue的观察数组变异方法 。You can use the double pipe operator to supply a default empty object for instances where
item.childOne
oritem.childTwo
are not defined.Change this:
{{Object.keys(item.childOne).length - Object.keys(item.childTwo).length}}
To this:
{{Object.keys(item.childOne || {}).length - Object.keys(item.childTwo || {}).length}}
Small scale example:
var test = {}; console.log(Object.keys(test.childOne || {}).length);
However, you should be mindful that Vue cannot detect property additions or deletions. As per the docs:
Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the data object in order for Vue to convert it and make it reactive.
Depending on your exact use case, you may need to define
childOne
during initialization or convert it to an array so that you may take advantage of Vue's observed array mutation methods.
相关问答
更多gulp-jscs Object.keys在非对象上调用(gulp-jscs Object.keys called on non-object)
在Vue.js App中初始化firebase 3.4.0(initialize firebase 3.4.0 in Vue.js App)
如何使用“Object.keys”函数在vue.js中列出firebase子节点的长度时防止出现错误?(How to prevent an error when listing the length of a firebase child node in vue.js using an “Object.keys” function?)
Object.keys(...)并不总是在IE中工作(Object.keys(…) not always work in IE)
从vue.js过滤器返回vue实例(Vue.js 2)(Return vue instance from vue.js filter (Vue.js 2))
Vue.js用于添加和编辑的相同表单(Vue.js same form for add and edit)
如何使用Object.keys()?(How to use Object.keys()?)
无法在vue.js 1.0和Laravel 5.1中处理错误(Can't get error handing in vue.js 1.0 and Laravel 5.1 to work)
Javascript:对象有键,但Object.keys返回空(Javascript: Object have keys, but Object.keys returns empty)
Vue.js图表不起作用?(Vue.js chart not working?)
相关文章
更多两种js function 声明方式
error C2668: 'M' : ambiguous call to overloaded function
Hadoop node 部署步骤
Hadoop下远程调试Child子进程
The connection to adb is down, and a severe error has occured.
Node.js视频教程
Hadoop Backup Node
Object Oriented Programming
Spark - A Fault-Tolerant Abstraction for In-Memory Cluster Computing
最新问答
更多Python / IPython奇怪的不可重现列表索引超出范围错误(Python/IPython strange non reproducible list index out of range error)
故事板从表格单元中延伸出来并显示其详细信息披露按钮(storyboard segue from a tablecell an its Detail Disclosure Button)
我们可以将Gherkin功能文件与testcomplete集成(Can we integrate Gherkin feature files with testcomplete)
TrustAllCertificatesCallback被忽略(TrustAllCertificatesCallback is ignored)
返回嵌套元素的索引(Return index of nested element)
在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)
Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)
Javascript错误:未捕获TypeError:无法读取null的属性'value'[duplicate](Javascript error: Uncaught TypeError: Cannot read property 'value' of null [duplicate])
使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)
如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!