Need help with wixlocation

Hello!

I´m trying to write a code to get information from a user input to search in a database, and when find this information, get the related dynamic page to this search.

Here´s the code:

export function button2_click(event, $w) {
let code = $w(“#input1”).value;
wixData.query(“BaseRV”)
.eq(“codigo”, code)
.find()
.then((results) => {
let item = results.items;
console.log(item);
==> The console.log here, bring the data that I need.

   wixLocation.to(item["link-BaseRV-title"]);  
   }) 

==> here I get an error: Wix code SDK error: The url parameter that is passed to the to method cannot be set to the value . It must be of type string.

   . **catch** ((err) => { 

let errorMsg = err;
console.log(errorMsg);
});
}

But when I write ==>

let item = results.items [some value];

I call the “item” in wixlocation and the dynamic pages appears.

Any tips?

Thanks in advance!

You say that this works:
let item = results.items [some value];
So, why don’t you use that?

Sorry, but I don’t understand what your problem is.

Yisrael

1 Like

Thanks for answer Yisrael!

When the user performs a search by code, this same code refers to a row in the database. One of the informations is the URL of the dynamic page, and this URL is what I need to send to the wixlocation. But I need to open the page corresponding to this code sent by the user, and not a specific data in line “result items”.

If I put, for example, [0] or [1] in result items, that will bring the first or second item of database. I need to bring the dynamic page refered to the code that user insert in the search field ( let code = $w(" #input1 ").value; )

I can give you more informations if you need…

Thanks in advance!

Sorry, but I don’t understand your problem.

How do you get to the dynamic page?
What do you have when you get to the dynamic page?
Where is the value that you need for the wixLocation.to()?

1 Like

In your code above you said that the console.log() showed the data that you wanted. Then why can’t just use that data?

1 Like

Ok, let’s make the question in a different way.

How do I, in this code, for wixlocation to work?

export function button2_click(event, $w) {
let valor = $w(“#input1”).value;
wixData.query(“BaseRV”)
.eq(“codigo”, valor)
.find()
.then((results) => {
let item = results.items;
console.log(item)
wixLocation.to (item[“link-BaseRV-title”]);
})
. catch ((err) => {
let errorMsg = err;
console.log(errorMsg);
});
}

The error is in the “wixLocation.to” line: Wix code SDK error: The url parameter that is passed to the to method cannot be set to the value . It must be of type string.

If you still need any information, we’ll try another way.

Thanks!

If the console.log() shows what you want, why can’t you just do:
wixLocation.to (item);
To be honest, that doesn’t make sense, but you said the console.log() was what you wanted, so I’m just proposing to use it as is.

If that’s not what you want, then what does the console.log() show that is the thing you want?

1 Like

That´s the log:

Loading the code for the site. To debug this code, open masterPage.js in Developer Tools.Loading the code for the GERADOR DE ALVOS page. To debug this code, open ns72x.js in Developer Tools.{“_id”:“97a495aa-0c69-482a-8a7d-0c8a51f8f976”,“_owner”:“c03eed67-f8b6-4cae-915e-378970ce16a5”,“_createdDate”:“2018-05-04T21:17:59.198Z”,“_updatedDate”:“2018-05-04T21:23:23.987Z”,“codigo”:“Q9X7-N03J”,“title”:“Diques de Castores”,“descricao”:“Os diques são construídos pelos castores para proteger-se dos predadores, tais como coiotes, lobos e ursos, e para poder ter acesso fácil e seguro à comida durante o inverno[42]. Por outro lado, a função primordial desta barreira é deter o fluxo da corrente, a fim de criar uma represa com águas tranquilas onde os castores possam construir sem dificuldades suas tocas[42]. Com frequência, os castores constroem um dique menor rio acima para diminuir a força da corrente e assim reduzir a pressão que exerce esta sobre a toca[44]. Costumam dar manutenção a todas as estruturas, com o que pouco a pouco vão aumentando de tamanho. Os castores podem reconstruir seus diques principais em decorrência de uma noite, ainda que podem não defender os diques secundários tão vigorosamente. Os castores são famosos por ter construírem diques muito longos[45]. O mais longo que se conhece foi descoberto perto de Three Forks, Montana, e media uns 652 m de comprimento, 4 m de altura e 7 m de grossura na base[46]. Também sabe-se que estes longos diques costumam ser obra de só umas poucas famílias de castores aparentadas, e a cada família não costuma passar dos dez membros. Porém, os diques pelo geral não medem mais de 1,5 m de altura e uns 3 m de largura na base, se fazendo mais estreitos para a parte superior[44]. O comprimento do dique geralmente depende da corrente do rio. Além de longos, costumam ser muito resistentes, pois podem suportar o peso de uma pessoa[32].”,“localidade”:“Estados Unidos”,“imagem”:“image://v1/c03eed_724297ee21b04f5ebb3041775b4bfa82~mv2.jpg/1024_768/1024px-Beaver_dam_in_Tierra_del_Fuego.jpg”,“link-BaseRV-title”:“/BaseRV/Diques-de-Castores”}GERADOR DE ALVOSLine 31Wix code SDK error:

The url parameter that is passed to the to method cannot be set to the value [object Object]. It must be of type string. (line 33)

line 33 ==> wixLocation.to (item);

console.log(item[“link-BaseRV-title”]);
What does the above line show in the console? Seems like that in fact is what you need.

Please post the URL of your site and I’ll take a look. And make sure you tell me what page this is on. Only authorized Wix personnel can get access to your site in the editor.

1 Like

Loading the code for the site. To debug this code, open masterPage.js in Developer Tools.Loading the code for the GERADOR DE ALVOS page. To debug this code, open ns72x.js in Developer Tools.
undefined

Page:

The site works like this: The user generates a code through the button “generate new target”, and the code appears where it says “target”. Then it copies the generated code and inserts it into the search field and presses “search target”. There is a field in the database for this matching code.

Sorry my site is in Portuguese :slight_smile:

Hey Carlos,

Try this (works for me):

export function button2_click(event, $w) {
   let valor = $w("#input1").value;
   wixData.query("BaseRV")
   .eq("codigo", valor)
   .find()
   .then((results) => {
      let item = results.items[0];
      let url = item["link-BaseRV-title"];
      console.log(url);
      wixLocation.to(url);
   })
   .catch((err) => {
      let errorMsg = err;
      console.log(errorMsg);
   });
}

Good luck,

Yisrael

1 Like

Hey Yisrael, the code works great! :slight_smile:

Thank you very very much!!

1 Like