Convert an object to text/string value

Does anyone know how to convert an object into a text value? I’m using a hook to combine multiple fields and I’m trying to include a reference field. All I can get it to show me is [object Object] for each referenced data point.

Any ideas?

export function EventPhotos_afterQuery(string) {
string.search = string.categories;
return string;
}

Hi,
What are you trying to achieve? What is the use case?
Please share more information so we can assist you.

1 Like

Hi, so I have two collections - one with events and one with categories. They have a many to many relationship. I’m trying to set up a database search field that can show results in my Events collection based on related Categories as well. So, if someone searches “weddings” for example, Events that include the word “weddings” in its category reference field will show up.

I’ve tried several things to accomplish this, like using the “include” function in my query, concatenating multiple queries (which doesn’t make sense for what I’m trying to accomplish), and I haven’t gotten anything to work. I decided that the best solution might be to make a combination field that strings together all keywords that I want to be searchable, including the referenced categories. Unless you can think of anything else that might work.

Hi @Or, do you have any ideas for me here? My use case is below.

Hi again, I’ve been playing with this some more and I feel like I’m close to having it working.

Here’s what I have for my data hook:

export function EventPhotos_afterQuery(item, context) {
item.search = item.categories
return item
}

Here’s what I’m able to get into my search cell:


It tells me that cell value type is JS object, but when I click “change to text” I lose all the data. Is there a way to just get this to be one long string? Really the only thing I’m interested in is “title,” can I just get those values converted to a text string?

@elena Hello did you ever find a solutions to JS object? I just want to return a single title.

Try something like this:
$w.onReady(function () {
$w(" #yourDataset “).onReady( () => {
let itemObj = $w(” #yourDataset ").getCurrentItem();
$w(’ #text 1’).text = itemObj.tba_no;
} );
});

Or you can try using JSON stringify to convert object to string, see links below for more details:

@ givemeawhisky Thank you for the information… I am sorry I should have been more clear in my comment. I am trying to get rid of the JS object, my field is suppose to be a reference field, and for some unknown reason to me it converted to a JS object… Any suggestion how to fix this?
I am thinking my reference field is not getting the context.

@emsimmons77 @givemeawhisky Eventually I just used JSON.stringify and a hook to copy over the data from the reference field into a plain text field. It seems to be working, though I’d really like to find a way to just select the title field and copy that over for each referenced item, rather than the entire row of data for each item.