簡體   English   中英

“使用無效數據調用函數 DocumentReference.set()。數據必須是 object,但它是”我如何解決? 在 nodejs 和 firestore

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

我嘗試添加使用另一個表 ID 獲取引用的數據,但是當我運行時顯示此錯誤。 在此處輸入圖像描述

我使用 MVC 架構

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;

路由器.js

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

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

 

 
module.exports = router;

更改.set(categoryID,name); 至:

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

.set需要一個帶有鍵/值對的 object,否則它不知道要更新哪個值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM