ToUpdate Database Tags (Array)

Hey,

I was wondering wether it was possible to add / remove tags in a database with the ToUpdate function.

I have a dataset called “menupermission” where I have a datafield with “permissions” that allow me to restrict pages to specific user roles. However, I want to create a page where values in “permissions” can be added or removed.

So far I have tried multiple things, but everything seems not to be formatted correctly as it returns:

I even tried to add a value that is already in the field but still it will not format correctly:

 $w.onReady(function () {
wixData.query("menupermission")       
                    .eq("title", "CRS")
                    .find()
                    .then((results) => {
                    id = results.items[0]._id
                    title = results.items[0].title
                    description = results.items[0].description
                    background = results.items[0].background
                    ondashboard = results.items[0].ondashboard
                    newtag = results.items[0].permissions + results.items[0].permissions[0]
                    })          
});

export function button1_click(event) {
updateje = {
                    "_id": id,
                    "title": title,
                    "description": description,
                    "background": background,
                    "ondashboard": ondashboard,
                    "permissions": newtag, 
                    }
}

export function button2_click(event) {
wixData.update("menupermission", updateje)
}

Thanks for any help in advance!

A tags type field contains an array. You will need to first get the tags array from the collection, add another element to the array, and then you can update.

1 Like

Thanks! @yisrael-wix

I found the solution with your help.

  1. First I called a query to retrieve the information that is relevant to my update:
import wixData from'wix-data';

var id = undefined
var title = undefined
var description = undefined
var background = undefined
var ondashboard = undefined
var updatedata = undefined
var updatedtags = undefined
var valueToInsert = undefined

$w.onReady(function () {
wixData.query("menupermission")       
                    .eq("title", "CRS")
                    .find()
                    .then((results) => {
                    id = results.items[0]._id
                    title = results.items[0].title
                    description = results.items[0].description
                    background = results.items[0].background
                    ondashboard = results.items[0].ondashboard
                    })
                    })

2a) In case an item needs to be added, I called the value I want to be added and used Push()

 valueToInsert = "Farmer"; // Value to be added
 updatedtags = results.items[0].permissions // Defines existing set
 updatedtags.push(valueToInsert); // Adds tag to existing set

2a) In case an item needs to be added, I called the value I want to be added and used Splice()

valueToInsert = "Head of MUN"; // Value to be removed
updatedtags = results.items[0].permissions // Defines existing set
updatedtags.splice(updatedtags.indexOf(valueToInsert), 1) // Removes tag from existing set
  1. Then I called the update
 updatedata = {
                    "_id": id,
                    "title": title,
                    "description": description,
                    "background": background,
                    "ondashboard": ondashboard,
                    "permissions": updatedtags, 
                    }
}
  1. I executed the update
wixData.update("menupermission", updatedata)
1 Like

:beers: