Writing image URL to another field

Hi Sebastian!

Im trying to use the scripting feature in Squides to write the an image URL from an assets field to a string field so that we can send that field to Algolia. The problem I’m facing is that I can’t seem to draw out the image-url to the string-field.

I have made a test schema with two field, and image/assets field and a string field:


The script:
var data = ctx.data;

if (data.imageTester && data.imageTester.iv) {
data.string = { iv: data.imageTester.iv[0].url };
}

replace(data);

When I set the string.iv to something else than an array it works just fint, but when trying to get the first object of the array (Since images gets returned as an array), it only comes back as undefined.

I would really appreciate you help in this.

Regards
Jonathan

Hi, the problem is that so far scripts only have only access to the raw data, so the list of asset ids.

In your case you can just generate the url manually

e.g.

var data = ctx.data;

if (data.imageTester && data.imageTester.iv) {
  data.string = { iv: `http://.../${data.imageTester.iv[0]}` };
}

Thank you Sebastian! That worked great! :blush:

1 Like