I use vue
and firebase
for my project. So initially, I'm sending some data to back-end in form of nested arrays (arrays within an array). Then firebase/firestore start yelling this:
Function DocumentReference.set() called with invalid data. Nested arrays are not supported.
After searching for solution online, I found this answer
So I initialized my data in vue that I want to send to back-end like below, the paths
array will store multiple arrays:
data() {
return {
product: {
images: [{paths: []}]
}
}
}
and save it to the back-end
this.product.images[0].paths.push([this.bigImgPath, this.thumbImgPath] // i.e push an array with 2 strings into the paths-array
And it throws me an error:
TypeError: Cannot read property 'paths' of undefined
This answer explains really well this problem, but I still get the error, even though I referenced the array's first element by this.product.images[0]
which stores an object with paths
as a key and empty array
as the value, then with dot notation access that key's value(empty array) and push my new array. How can I successfully add the array with two strings (bigImgPath, thumbImgPath)
into my nested data structure? Thank you !
EDIT: reproducible code
new Vue({
el: "#app",
data: {
product: {
images: [{paths: []}]
},
bigImgPath : "/path/src/big.jpg",
thumbImgPath : "/path/src/thumb.jpg"
},
methods: {
myFunc: function(todo){
this.product.images[0].paths.push([this.bigImgPath, this.thumbImgPath])
console.log(this.product.images[0].paths[0][0])
console.log(this.product.images[0].paths[0][1])
document.getElementById("first-text").innerHTML = this.product.images[0].paths[0][0]
document.getElementById("second-text").innerHTML = this.product.images[0].paths[0][1]
}
}
})
.first-text {
font-size: 18px;
font-weight: bold;
}
.second-text {
font-size: 18px;
font-weight: bold;
}
.main-btn {
padding: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<html>
<body>
<div id="app">
<div id="main">
<p id="first-text"></p>
<p id="second-text"></p>
<button @click="myFunc()" class="main-btn">Press me!</button>
</div>
</div>
</body>
</html>