function ProductsServices_beforeInsert(item) {
const fieldValue1 = item.make;
const fieldValue2 = item.model;
// Concatenate the values
const concatenatedValue = fieldValue1 + " " + fieldValue2;
// Update the item with the concatenated value before inserting it into the collection
item.makeModel = concatenatedValue;
console.log(item);
return item;
}
Try this
by the way you can just concatenate the 2 objects like this:
item = {
"make": "make",
"model": "model",
"makeModel": ""
};
item.makeModel = item.make + " " + item.model
OK. Where do I put this code ?
is your code in the backend or frontend?
are you going to use wix-data and write the item to any collection?
I am writing to a single collection when I edit the collection directly through the CMS
just use the
item = {
"make": "make",
"model": "model",
"makeModel": ""
};
item.makeModel = item.make + " " + item.model
whenewer you need it there is no need for a function
OK, where do I place this code ? In a hook ?
so lets say you want to write to the collection just use
export function addToCollection(item){
item.makeModel = item.make + " " + item.model;
wixData.save('Cars', data).then(() => {
console.log("Writing Successful. Make Model: " + item.makeModel)
} ).catch((err) => {
console.error(err);
});
}
Note this will give an error becouse yo are using reference for your make field
OK. How do I avoid the error ?
How do I call this function when the item is added or updated ?
export function addToCollection(item){
let makeID;
wixData.query("makes")
.eq("tittle"), item.make)
.find()
.then((results) => {
if(results.items.length > 0) {
makeID = results.items[0]._id;
} else {
console.err('no make found by name' + item.make)
}
})
.catch((err) => {
console.log(err);
});
item.makeModel = item.make + " " + item.model;
let data = {
"make" : makeID,
"model": item.model,
"makeModel": item.makeModel,
}
wixData.save('Cars', data).then(() => {
console.log("Writing Successful. Make Model: " + item.makeModel)
} ).catch((err) => {
console.error(err);
});
}
and call the function whenever you want it to work
Sorry, I am brand new to Velo and Wix.
How do I call it when a new item is added ?
where it is going to be added?
I am adding items manually directly to my collection via the CMS.
I was getting undefined undefined earlier which seemed like progress but now I am getting nothing.
becouse we didnt call the function
can you share an screenshot?
can you show me your collections?