Concatenate names within database while ignoring empty cells

Hey everyone. Like a lot of others here, I am excellent at excel but new to coding so any help would truly be appreciated. I have a membership database within our site that every so often has to be exported and sent elsewhere for records purposes. Because of this, instead of using code on the front end on specific pages to show a combined name, I created a column within the database where an afterQuery code is used to combine the members complete name (prefix, first, middle, last, and suffix).

This has worked as expected however now I am running into the issue where “null” is also being added into the name for the fields of data where a person may not have a prefix, middle name, or suffix. Is there additional code that I could add to help keep the “null” out of the name? From what I searched in the forums, a few answers were only related to when the name was combined and displayed on a page. Unfortunately, I need to figure it out for the database end. Thank you for any help you can provide. Below is a snap shot of the code and the results to give you an idea where I am at.

Hey Chad!

Adding string.replace("null ", “”); should work if you can make your concatenated item into a variable before returning it.

Chad, I had worked out a solution, but then saw David’s post and thought “oh, a better, more concise way of doing it.” Then, I tried it and couldn’t make it work. Maybe, I’m missing something? In any event, the following did achieve the desired result. Interestingly, I’m not sure why I’m getting undefined when the hook writes to the collection and you’re getting null with approach A, so substitute null for undefined as needed in the “if” statements.

export function RegHistory_afterQuery(item, context) {
 let cFullName = '';
 if (item.prefix !== undefined) {
        cFullName = item.prefix.trim() + ' ' + item.firstName.trim();
    } else {
        cFullName = item.firstName.trim();
    }
 if (item.middleName !== undefined) {
        cFullName = cFullName + ' ' + item.middleName.trim() + ' ' + item.lastName.trim();
    } else {
        cFullName = cFullName + ' ' + item.lastName.trim();
    }
 if (item.suffix !== undefined) {
        cFullName = cFullName + ' ' + item.suffix.trim();
    }
    item.fullName = cFullName;
 return item;
}

There´s a difference between Null and undefined. To catch them all, simply test like:

if(item.prefix) {

etc

This means: if item.prefix is not Null, not undefined and not an empty string, then …

Thanks everyone. This has helped a lot. I ended up using Anthony’s version while modifying it with Giri’s suggestion to catch all possible issues. So far, it is working as needed. Here is the modified code I used:

export function Members_All_afterQuery(item, context) {
let cFullName = ‘’;
if (item.prefix) {
cFullName = item.prefix.trim() + ’ ’ + item.firstName.trim();
} else {
cFullName = item.firstName.trim();
}
if (item.middleName) {
cFullName = cFullName + ’ ’ + item.middleName.trim() + ’ ’ + item.lastName.trim();
} else {
cFullName = cFullName + ’ ’ + item.lastName.trim();
}
if (item.suffix) {
cFullName = cFullName + ’ ’ + item.suffix.trim();
}
item.fullName = cFullName;
return item;

I have used this code to no current avail

export function Member_afterQuery(item, context) {
item.fullDobdod = item.dob + " " + item.dod;
let fullName = ‘’;
if (item.nickName !== undefined) {
fullName = item.nickName.trim() + ’ ’ + item.subPrefix.trim();
} else {
fullName = item.subPrefix.trim();
}
if (item.subPrefix !== undefined) {
fullName = item.subPrefix.trim() + ’ ’ + item.firstName.trim();
} else {
fullName = item.firstName.trim();
}
if (item.middleName !== undefined) {
fullName = fullName + ’ ’ + item.middleName.trim() + ’ ’ + item.lastName.trim();
} else {
fullName = fullName + ’ ’ + item.lastName.trim();
}
if (item.suffix !== undefined) {
fullName = fullName + ’ ’ + item.suffix.trim();
}
item.fullName = fullName;
return item;
}

Any suggestions welcome
Thanks
Adam

Adam,

I’m guessing that the subPrefix field has no value. So the first if statement fails in either case. Whether or not the nickName test passes, it tries to obtain the value of subPrefix and it may be undefined, null or an empty string (user entered something and then deleted it). Also, it’s better to use Giri’s approach above to account for all three possibilities.

One of the mistakes i made here was to not identify each item before let fullName =
therefore the console code e.g. cannot read property was correct.
subPrefix could have a value as this is connected as a field on a different page. The purpose of this js file is to identify those fields that have been populated or not to produce a fullName string when called

anyway @tony-brunsman thanks for your response, it’s always very helpful to have you ninja’s around to offer support and guidance.

I’m not sure if you can see other posts i’ve made to the community but there are a couple of outstanding issues i could really do with some help with.

Thanks
Adam

Hi Giri how would this actually work as a code snippet please for fullName with several fieldkeys

your help would be greatly appreciated
thanks
Adam

ok i’ve played with this code in many different ways currently i have this which shows on my repeater page both prefix and firstName and for older records where prefix wasn’t an option at the time undefined, firstName, undefined etc.

export function Member_afterQuery(item, context) {
let fullName = “”;
if (item.prefix) {
fullName = item.prefix;
}
if (item.subPrefix) {
}
if (item.addOwnPrefix) {
}
if (item.firstName){
}
if (item.middleName){
}
if (item.lastName){
fullName = item.prefix + " " + item.subPrefix + " " + item.addOwnPrefix + " " + item.firstName, + " " + item.middleName, + " " + item.middleName;
}
item.fullName = fullName;
return item;
}

I have tried so many different variations and get mixed results for some field keys and not others, i just can’t see how to catch all.

Regards
Adam

These brackets { } designate a new block for your condition, so if the condition is true, whatever is inside the two brackets will run. Since your brackets are empty, it’s simply not going to do anything if those conditions are met since that’s what you told it to do. You can have one if statement with multiple conditions to check like this:
if (item.prefix && item.subPrefix && item.addOwnPrefix) {
fullName = item.prefix + " " + item.subPrefix + " " + item.addOwnPrefix + " " + item.firstName, + " " + item.middleName, + " " + item.middleName;
return item;
} else return ‘error’;

So just add your other conditions to the if statement and you can handle your error bevavior in the else statement.

Thanks David, I added your code to my fullName js file and really thought this would work but all my repeater items (with fullName text element linked to fullName in collection) were empty and the console had the following information

Loading the code for the My Profile page. To debug this code, open xx4w3.js in Developer Tools.
Hook afterQuery for collection Member result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [String]

Regards
Adam

export function MemberCandles_afterQuery(item, context) {

let fullName = “”;
if (item.prefix && item.subPrefix && item.addOwnPrefix && item.firstName && item.middleName && item.lastName && item.suffix && item.addYourOwnSuffix) {

fullName = item.prefix + " " + item.subPrefix + " " + item.addOwnPrefix + " " + item.firstName + " " + item.middleName + " " + item.middleName + “” + item.lastName + “” + item.suffix + “” + item.addYourOwnSuffix;
return item;
} else return ‘error’;
}

ERROR
Hook afterQuery for collection Member result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [String]

@skmedia Hi David, i would massively appreciate your help to continue with this problem, i just can’t see where i am going wrong

Thanks
Adam

Your above code checks that all columns are not empty and if any are (the whole point of this exercise is that you expect some will be empty) you try to overwrite the entire returned database row with the string ‘error’. As the error states (as does the hooks documentation), this is no bueno.

JD in another thread (https://www.wix.com/corvid/forum/main/comment/5df2c1eaad09f60017b5012b) gave you code that was far closer to what you ought to be doing. This if() soup you’ve been writing is typical of beginners and while it can definitely produce the correct result, it is not good quality code. You shouldn’t be repeating the same code again and again to perform the same task. It’s harder than it needs to be to understand, is harder to maintain if you need to change anything, and as you’ve experienced is prone to errors if you don’t copy things correctly.

My opinion is that if you want to pursue coding, you ought to learn plain Javascript away from Wix, where all the key concepts are explained in depth and this will give you a more solid foundation on which to build. Blind trial and error won’t get you very far. There are many free resources that will help you do this.

export function MemberCandles_afterQuery(row)
{
    row.fullName = ['prefix', 'subPrefix', 'addOwnPrefix', 'firstName', 'middleName', 'lastName', 'suffix', 'addYourOwnSuffix'] //these are all the column names we're interested in
        .reduce((accumulator, key) => //reduce loops over an array and eventually returns a single value, key will contain the values from the line above
        {
            if(row[key] && row[key].trim()) //if cell is non-empty and remains so after trimming whitespace
                accumulator += row[key].trim() + ' '; //add it to our accumulator with a space appended

            return accumulator;
        }, '') //start our accumulator with an empty string
        .slice(0, -1); // we don't want to keep the final space we added to the last non-empty column

    return row; //return the original row queried from the database with our additional fullName column
}

@David - SKmedia Perhaps you’ve realised this in the intervening nine months, but your initial post in this thread is objectively wrong. You don’t string replace null out of user-supplied input - you prevent null from being caught up in these strings in the first place. Maybe there aren’t many names in the world that contain the substring null, but for those that do your code would mangle them.

1 Like

@adcatch02 Since it’s a hook, you must make sure to insert your new string as the correct property.

Lee’s code is much more concise than what you are trying to do and should work because it retains the row’s object structure and contains the _id.

This is an amazing section of code thank you so much Lee

@skmedia Thanks David, i have just implemented Lee’s code and it works perfectly thanks so much for replying
Adam