Storing an array in Firestore

I am currently struggling with using Firestore to store data on a webpage. I am attempting to store an array of custom objects, but for some reason Firestore is not allowing me to do so. I have attempted to pass it as an object of objects, but it keeps getting rejected. Can anyone guide me on how to accomplish this task? Below is the code snippet I am working with:

function Book(title, author, pages, read) {
    this.title = title;
    this.author = author;
    this.pages = pages;
    this.read = read;
}

let myLibrary = [
    (new Book("1984", "George Orwell", 123, "Read")),
    (new Book("Lord of the Rings", "JRR Tolkien", 300, "Read")),
    (new Book("Neuromante", "William Gibson", 200, "Read"))
];
var firestore = firebase.firestore();
const docRef = firestore.collection('books');
docRef.doc('myLibrary').set(myLibrary);

Answer №1

One notable aspect is that Javascript lacks strong typing, which means JSON also lacks this feature. Therefore, when dealing with storing book objects in Firestore, it's necessary to convert them into vanilla JSON format for proper storage.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Return to the previous URL after executing window.open in the callback

I need help redirecting the callback URL back to the parent window that initially called window.open. Right now, the callback URL opens inside the child window created by the parent. Here's the scenario: The Parent Window opens a child window for aut ...

What is the best way to showcase multiple details within a single component in React Native?

When I click on any of the main tabs under "Categories", I want only that tab's details to appear. Initially, I declared a flag for each category and updated it when clicked to show the details. However, this approach seems flawed as clicking on one ...

Unleashing the power of the open form reveals a newly resized window

<script type="text/javascript"> function openSmallWindow() { .. // Specify URL, window size and attributes window.open('http://isivmwebtest.isolutionsinc.com/ComparativePricing/Content/PriceLookup.aspx','windowNew',&ap ...

Rendering Vue components synchronously as a singular string

There exists a Vue SFC called Bubble, which contains a simple layout structure. Bubble.vue <script setup lang="ts"> </script> <template> <div hinted-name="wrapper-bubble" class="hinted-bubble-wrapper" ...

Tips for looking up a class within the document and assigning it to a different element using JavaScript?

As someone new to javascript, I'm eager to create a code that will consistently search for a specific div class in an HTML file and then assign another class to my navigation if the required class is found. Although I attempted to write the code mysel ...

Assign a value to an element based on a specific attribute value

I'm working with the following element <input type="text" size="4" name="nightly" value="-1"> My goal is to update the value to 15.9 specifically when name="nightly" Here's what I've attempted so far: document.getElementsByName(&ap ...

Uh oh: encountered an unexpected token 'export' in node_modulesexpoAppEntry.js

I'm encountering a challenge with my Expo project. While attempting to launch my app using yarn start, I'm running into this error: Android Bundling failed 449ms error: node_modules\expo\AppEntry.js: Unexpected token 'export&apo ...

What is the best way to sum all the values associated with the `index` key in a PHP Array?

I'm working with PHP and have an array that looks like this: $nnarray = Array ( [9] => Array ( [1] => Array ( [2] => 433 [6] => 2 [4] => 101 ) [3] => Array ( ...

Verifying if an array has identical key-value pairs as another array being looped through in PHP

At the core of my application is Laravel 5.3 My task is to streamline queries in my restaurant web control panel for editing dishes in daily special menus. The preview of the view is displayed below: https://i.sstatic.net/6Z8uY.png I have two separate q ...

Set a maximum limit for the number of checkboxes that can be selected

If there are 10 checkboxes on a page, I am searching for a specific behavior: Use a variable called maxSelections to control the maximum number of selectable checkboxes Once maxSelections is reached, disable the other checkboxes on the page Even after re ...

To incorporate an if-else statement into an onClick event depending on the selected radio button option

My page currently loads two separate lists of data as cards from different URLs: http://localhost:3000/videos http://localhost:3000/manuals I have a div that displays both lists together and a "Create Card" button that opens a modal. After filling in the ...

Filtering an array can be done by selecting a specific item and applying the

An array of objects is present in a JSON file: [ { "department": "Sound Department", "subDepartments": [ "Supervising Sound Editor", "Dialog Editor", "Sound Designer", "Foley Artist", ...

React click event not being applied to the surrounding elements

Within my React component, I have the following structure: <div class="iconButton" onClick={clickEvent}> <MyIcon /> </div> The MyIcon component utilizes an SVG file and displays it on the screen. A hover effect is tri ...

How to retrieve an element using a dynamically generated class name in Vue.js

<v-data-table :headers="menuheaders" //this menus from api response :items="menus" item-key="usersmenu_menuid" items-per-page="1000" hide-default-footer="" class="elevation-1" > <template v-s ...

JavaScript utilizing a PHP variable

In my attempt to merge a PHP variable with JavaScript, I aim to access the "the_link_to_the_page_I_want". window.onload = function openWindow() { window.open('the_link_to_the_page_I_want', 'newwindow', config='height ...

What is the best way to show multiple markers using react-google-maps?

Can anyone help me figure out how to display multiple markers using the react-google-maps npm package? I've been following a demo that shows how to display one map marker, but when I try to add a second marker element, it doesn't show up on the m ...

What is the best way to transform a date such as '2021-06-01T11:17:00-07:00' into the corresponding date and time for a specific location (New Delhi) using JavaScript?

How can I convert a date in the format '2021-06-01T11:17:00-07:00' to ISO standard? What does this format represent? let date = new Date('2021-06-01T11:17:00-07:00'); console.log(date.getHours() + " :" + date.getMinutes()); I ...

Pass a jQuery variable to the current file and retrieve it using PHP

Is there a way to send jQuery data and receive it on the same file without including it in the URL as a variable (e.g. test.php?data=xxx)? For instance, here is the response.data that can be seen in the console log: function (response) { ...

Access the second argument in the method `document.getElementById("box5" && "box14")`

I need help with a script that should set the variable trouve_coupable to true only if both box 5 and box 14 are checked on my HTML page. However, no matter what I do on the page, whenever box 14 (="case14") is checked, it always returns true whe ...

Using ReactJS and JavaScript to transform an array into a fresh array

I am working with an array that looks like this: var oldArray = [ {number: '12345', alphabet: 'abcde'}, {number: '54321', alphabet: 'abcde'}, {number: '67891', alphabet: 'abcde'}, ...