M
M
Maksim Herasim2021-07-02 11:32:51
JavaScript
Maksim Herasim, 2021-07-02 11:32:51

JS IF ELSE in JSON post request, how to implement?

Data comes from the site form, I parse it and get data in json format, then I need to check whether the field is filled or not, if it is filled then pass it on in the post request, if not filled then do nothing.
The input looks like this -
60decaef121e0480031523.png
I want to do a check, IF\ELSE.
If form.NAME = noone then nothing
Else "NAME" : form.NAME;
My script looks like this -

//Объявляю переменную
var formname = msg.form.NAME
//Служебная информация метода, url, тип данных
msg.headers = {}
msg.method = "POST"
msg.url = "http://127.0.0.1:1880/tst" ;
msg.headers["content-type"] = "application/json"
//Сам скрипт в msg.payload и есть тело запроса
msg.payload = {
    //если formname = noone, то пропускаем и ничего не делаем
    if (formname = 'noone') {
        //
    }
    //Иначе добавляем в тело "NAME" : formname
    else {
      "NAME": formname;  
    }
// Это просто данные в формате JSON
"PHONE":"+79876543210",

};
//возвращаем в поток
return msg


But my script doesn't work, the validator throws errors
60decf1c4dda6001722072.png
60decf29e07c4540597472.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-07-02
@Tkreks

if is a language command, it cannot be inside a data declaration.

msg.payload = { PHONE: '+79876543210', };
if (formname !== 'noone') {
  msg.payload.NAME = formname;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question