Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

javascript - Update value in nested field in object - Firestore

I'm trying to update a nested field in Firestore and I'm somehow overriding the entire object. A map of my data is shown below: enter image description here

And I try to update rundate and status with this command:

 //Assume deptName is extrusion

    const item = await db.doc(`orders/${orderId}/batches/${batchNumber}`)
   .update({
     [`batchDeptStatusInfo.${deptName}`]: {
      status:"Scheduled",
      rundate: newRunDate,
    }
   }) 

However, this overwrites the entire object. As seen above, conversion has four properties, while extrusion (the property I tried to update) only has 2.

Can anyone provide some guidance on this issue? I've seen posts that say use dot notation and update not set and I think I check those boxes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you specify a new value for a field, the value you specify will replace the entire value for that field. The server doesn't perform any sort of patching between the existing value and what you specify.

If you want to perform granular updates, make sure to specify the entire path to where you want to set the value.

So:

await db.doc(`orders/${orderId}/batches/${batchNumber}`)
 .update({
   [`batchDeptStatusInfo.${deptName}.status`]: "Scheduled",
   [`batchDeptStatusInfo.${deptName}.rundate`]: newRunDate
 })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...