BTS-Dev
BTS-Dev

Reputation: 33

Firestore Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs

I am trying to add a new record like the one below to a Firestore document using a function deployed to firebase. The function is developed in typescript:

Function:

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const app = express();

app.put('/v2/MYENDPOINT/add', async (req, res) => {
    try 
    {
        const res1 = db.collection('Bars2')
                         .doc('NHn8368LhIhNWHTOaEDo')
                         .update({
                            name: req.body['name'],
                            address: req.body['address1']
                          }).then(function() {
                            res.status(200).send(`Created a new Real bar2: ${(res1)}`)
                        })
                        .catch(function(error) {
                            res.status(400).send("Error writing document: " + error);
                        });


        //res.status(200).send(`Created a new Real bar2: ${(res1)}`)
    } 
    catch (error) 
    {
        res.status(400).send(`Real bar2 not created!!! ${error}`)
    }        
})

I try with postman to add this item but the code is triggering an error:

Postman New Request { "name": "BAR C", "address": "qwerty" }

Real bar2 not created!!! Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore value. Cannot use "undefined" as a Firestore value (found in field "address"). If you want to ignore undefined values, enable ignoreUndefinedProperties.

Firestore structure is in the image attached.

Any ideas what I am missing? Thanks

enter image description here

[How I Would Like the Data To Look On Right Side Of Image

Upvotes: 3

Views: 8828

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

The key to the error message is this:

Cannot use "undefined" as a Firestore value (found in field "address").

Your address field is undefined, which is not a valid value for Firestore documents. That means req.body['address1'] is undefined.

Perhaps you meant to say "address" instead of "address1".

Upvotes: 6

Related Questions