String.prototype.replaceAll() undefined

I have succesfully used the code snippet below in my onBeforeInsert hook to replace all spaces from a value:

    item . song_seoname  =  item . song_name . replaceAll ( " " ,  '-' ); 

I am pretty sure has worked before, as I have sucessfully inserted multiple values with all spaces replaced with “-”

But this piece of code didn’t work anymore (also not when moved to Page Code).
As it turns out String.prototype.replaceAll() function is undefined in the Wix namespace.

This Javascript feature has been supported by all major browsers for quite some time now.
Why has it gone missing from Wix and what version of Javascript are we really using in Wix?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

Bug… just wait… your code won’t dead :slight_smile:

Other way to hide the red line
Off the TS check for the one next lint
// @ts-ignore

Add the comment to that line is ok

@certified-code : I’ve added the line and it works.
I’ve checked at least on Chrome Version 90.0.4430.212
Example:

// @ts-ignore
str = str . replaceAll ( ’ ’ , ‘-’ );

Good to hear that!:grinning:

replaceAll() does not work on Wix

It seems it is working in Page Code, but it is not working in backend code.
Tried my own backend/application.jsw and the backend/data.js
Functional testing shows the error. If I invoke this code from page code, I only get “ERROR” in Site Monitoring.

export function test (){
var teststring = “my string”
var result = teststring . replaceAll ( " " , “-” );
}

.replaceAll is not a valid method on Node version 14 (which is the current version installed on Wix backend).

Use an alternative such as:

var teststring = "my string";
var result = teststring.split(" ").join("-");

@jonatandor35 Thanks very much for your answer, that explains alot!

Do you also happen to know when Node 15 (released 2020-10-20) will be used in Wix?

Perhaps the docs should inform devs about the fact that serverside javascript uses an older Javascript engine than the browser?
Could have saved me some time :roll_eyes:

@ronald28659 As far as I know Node 15 will never be installed because it is not an LTS release. The next one will be Node 16 (I don’t know the ETA).

@jonatandor35 I have tried replace and split and join in my backend code to replace slashes "" in some of my strings and it does not seem to work? Anyone else have this issue?

@jonatandor35 oh and I can replace any other character just now a forward slash. Here is below a sample test code I have been playing with. Also, another weird thing is the first log statement doesn’t even show all the slashes “"? This is the message from the log " [“Test:”,”\this is a \test \this is a \test"]".

let regexg = /\/ gm ;

    **var**  test  =  '\th\is \is \a \te\st \this \is \a \test' ; 

    console . log ( "Test   :"  ,   test ); 
    **var**  sales  =  test . split ( regexg ). join ( '' ); 

    console . log ( "Test:"  ,   test ); 
    console . log ( "Sale:"  ,   sales ); 

    sales  =  test . split ( '\\' ). join ( '' ); 

    console . log ( "Test2:"  ,   sales ); 

    sales  =  test . replace ( '\\' , '' );