"Function DocumentReference.set() called with invalid data. Data must be an object, but it was" How I solve it? In nodejs and firestore

Viewed 8

I tried to add data getting reference with another table Id but when I run then show this error. enter image description here

I used MVC architecture

controller.js

const addProduct = async (req, res, next) => {
    try {
        const  {categoryID,name} = req.body;
        await firestore.collection('product').doc().set(categoryID,name);
        res.send('Word saved successfully');
    } catch (error) {
        res.status(400).send(error.message);
    }
}

model.js

class Product {
    constructor(id, name,categoryID ) {
            this.id = id;
            categoryID =  {
                type : firefose.Schema.Types.ObjectId,
                ref : 'category',
                required : true
            },
            this.name = name;
          
    }
}

module.exports = Product;

router.js

const router = require("express").Router();
const { addProduct } = require("../controllers/words12controller.js");
 

router.post('/addProduct', addProduct);

 

 
module.exports = router;
1 Answers

Change .set(categoryID,name); to :

.set({ category: categoryID, name: name});

.set expects an object with key/value pairs otherwise it doesn't know which value to update.

Related