What is the correct way to condition a hook on an empty input?

Hello everyone, I have a question, can a hook be conditioned? how is it done? and for an empty input? In my case, specifically, I have a hook in a form, so that people can enter only their social network username, and the hook is responsible for adding the corresponding URL as a prefix, for example:

User: jonathan77
URL in the hook: “https://www.face book.com/” (no spaces)
result in the collection field (collection and input Type text and not Link, due to URL field cant be leave it blank o a single letter): https://www.face book.com/jonathan77

The code works very well, however, if any user does not use a social network, “undefined” is automatically added as a user, leaving an “operative” link like this: “https://www.face book.com/undefined”, if you send the form for the second time and if the “fbUser” field is empty (this is the name of the field ID in the collection) just the prefix is also added, sent to whoever clicks on my “custom icon” to the main website of that network (“https: //www.face book.com/”), so can the hook be conditioned so that in case the form input is left empty, or just an “X” is added to the collection field instead of the URL prefix?

Thanks, for any help or any idea to test

Collection name: Brokers
Field in the collection where the user is entered: “fbUser”
Field in the collection where the full URL goes: “facebookUrl”
Name of the input in the form #inputFb

export function Brokers_beforeInsert(item, context) {
	let nullUrl = "X" //←Does nothing
	let URLfb = "https://www.face book.com/"

if (item.fbUser === undefined ||
   		item.fbUser === null ||
   		item.fbUser === ''){
	 item.facebookURL = nullUrl;
	}else{ item.facebookUrl = URLfb + (item.fbUser)};
  	return item;
}

PD: Its has a Brokers_beforeUpdate exported version too, same as Brokers_beforeInsert

Hi there,

As long as the associated collection and field names are correct, the following code seems mostly correct if the intention is to conditionally set the facebookUrl field depending on whether a username was provided or not.

I would check the data type returned for when “undefined” is automatically added into the item object because it is possible the right condition isn’t being branched to since undefined does not equal “undefined” in JavaScript (assuming the result is a String).

For more information on Data Hooks, check out the following tutorial:

Velo Tutorial: Processing User Input Before it is Stored in a Collection with Data Hooks

Thanks for your answer, I am very new to code, even so I did the code based on that tutorial that you recommend, and still, the hook is in charge of joining 2 components, the corresponding URL prefix + the user placed in the input, if the input is left empty, the hook also adds the URL prefix to the collection item and I don’t want that, I want it to also be empty (or with an “X”) too, it’s as if it completely ignored the conditions that I’m telling it.

Should I rather tell it that instead of checking “if” there is an item in the “fbUser” collection field, rather check if something was added in the input? something like this:

if (item.$w(#inputFb)) === undefined || // ←Im not sure how to put that!
   		item.$w(#inputFb) === null ||
   		item.$w(#inputFb) === ''){
	 item.facebookURL = nullUrl;
}else{ item.facebookUrl = URLfb + (item.fbUser)};
  	return item;
}

I am not sure what is happening, what I do know is that something is wrong with the conditions, because they are ignored

I dont know how to do that check, can you tell me please?

Try out the following:

If the property doesn’t show up in the data hook item, you can check if the property doesn’t exist by directly referencing it.

Example (Property not present in Data Hook’s Item Object)

if(!item.fbUser){
   //Handle Null URL
}

If the property for the empty form field does show up in the data hook item, check its value against a string.

Example (Property present in Item Object)

if(item.fbUser === "undefined"){
   //Handle Null URL
}

To verify which case it is, you can use Site Logs and print the item object inside of the beforeInsert data hook. This should allow you to verify the correct structure for the item object across the different cases.

1 Like

Ok @thomasj One last question, do you know who is amazing? YOU ARE!

I tried both examples, at first none worked and it was due to a capital letter that escaped me and I didn’t see during these tests (have to be careful, definitely), finally this was the correct one, the first one:

export function Brokers_beforeInsert(item, context) {
	let nullUrl = "X" 
	let URLfb = "https://www.face book.com/" // No spaces
if(!item.fbUser){ 
   		
	 item.facebookUrl = nullUrl;
	  }else{ item.facebookUrl = URLfb + (item.fbUser)}
	return item;
}

↑ Same for export function Brokers_beforeUpdate(item, context) ↑

I don’t know how to make verifications or use the “Site Logs” or “print the item object inside of the beforeInsert data hook”, I was following the steps of the material that you indicated, I see the “receiveTimestamp”, the “jsonPayload.message” which It tells me that to debug I must open a “vw0cp.js” file that I don’t know what it is, or where it is, if I should create it or if it already exists, and I see the “severity” that in one of the tests gave me an error in the dataset that I will look at, in the same way, thank you very much

2 Likes