Database Query Has No Results

I am attempting to query my database called battery2221 but keep getting no results. I’ve read all the API documentation but can’t seem to find the problem. Any feedback is greatly appreciated. Here is my page code:

import { getVINInfo } from 'backend/VINModule';
import wixData from 'wix-data';

$w.onReady(function () {
    //TO DO: Write Your Page Related Code Here:

})

export function button1_click(event, $w) {
    //Add your code for this event here:
    getVINInfo($w("#vininput").value)
        .then(VINInfo => {
                console.log(VINInfo)

                let year = VINInfo.Results[9].Value;
                let make = VINInfo.Results[6].Value;
                let model = VINInfo.Results[8].Value;
                let engine = VINInfo.Results[71].Value + " " + "Liters"
                let car = VINInfo.Results[9].Value + "  " + VINInfo.Results[6].Value + "  " + VINInfo.Results[8].Value;

                wixData.query("battery2221")
                    .include("title")
                    .eq("year", year)
                    .eq("make", make)
                    .eq("model", model)
                    .find()
                    .then((results) => {
                            console.log(results);

                            $w("#results").text = "Vehicle Specific Battery for Your" + " " + car;

1 Like

Hi there :wave:t2: On this line where you console log the VINInfo from your backend function do you get results in the console?

console.log(VINInfo)

But on this line here you do not get results, correct?

console.log(results);

That’s correct. Here is what the developer console shows in preview mode for reference:

{
  "_items": [],
  "_totalCount": 0,
  "_query": {
    "orderBy": "undefined",
    "invalidArguments": [],
    "filterTree": {
      "$and": [
        {
          "year": "2019"
        },
        {
          "make": "HONDA"
        },
        {
          "model": "Civic"
        }
      ]
    },
    "provider": {
      "save": "function e(){return t.getJSON(\"/wix/data-web.jsw/save.ajax\",arguments)}",
      "insert": "function e(){return t.getJSON(\"/wix/data-web.jsw/insert.ajax\",arguments)}",
      "update": "function e(){return t.getJSON(\"/wix/data-web.jsw/update.ajax\",arguments)}",
      "get": "function e(){return t.getJSON(\"/wix/data-web.jsw/get.ajax\",arguments)}",
      "distinct": "function e(){return t.getJSON(\"/wix/data-web.jsw/distinct.ajax\",arguments)}",
      "truncate": "function e(){return t.getJSON(\"/wix/data-web.jsw/truncate.ajax\",arguments)}",
      "remove": "function e(){return t.getJSON(\"/wix/data-web.jsw/remove.ajax\",arguments)}",
      "find": "function e(){return t.getJSON(\"/wix/data-web.jsw/find.ajax\",arguments)}",
      "runAggregate": "function e(){return t.getJSON(\"/wix/data-web.jsw/runAggregate.ajax\",arguments)}",
      "count": "function e(){return t.getJSON(\"/wix/data-web.jsw/count.ajax\",arguments)}",
      "bulkInsert": "function e(){return t.getJSON(\"/wix/data-web.jsw/bulkInsert.ajax\",arguments)}",
      "bulkSave": "function e(){return t.getJSON(\"/wix/data-web.jsw/bulkSave.ajax\",arguments)}",
      "bulkUpdate": "function e(){return t.getJSON(\"/wix/data-web.jsw/bulkUpdate.ajax\",arguments)}",
      "bulkRemove": "function e(){return t.getJSON(\"/wix/data-web.jsw/bulkRemove.ajax\",arguments)}",
      "queryReferenced": "function e(){return t.getJSON(\"/wix/data-web.jsw/queryReferenced.ajax\",arguments)}",
      "insertReference": "function e(){return t.getJSON(\"/wix/data-web.jsw/insertReference.ajax\",arguments)}",
      "insertReferences": "function e(){return t.getJSON(\"/wix/data-web.jsw/insertReferences.ajax\",arguments)}",
      "replaceReferences": "function e(){return t.getJSON(\"/wix/data-web.jsw/replaceReferences.ajax\",arguments)}",
      "removeReference": "function e(){return t.getJSON(\"/wix/data-web.jsw/removeReference.ajax\",arguments)}",
      "removeReferences": "function e(){return t.getJSON(\"/wix/data-web.jsw/removeReferences.ajax\",arguments)}",
      "isReferenced": "function e(){return t.getJSON(\"/wix/data-web.jsw/isReferenced.ajax\",arguments)}",
      "getSchema": "function e(){return t.getJSON(\"/wix/data-web.jsw/getSchema.ajax\",arguments)}"
    },
    "collectionName": "battery2221",
    "limitNumber": 50,
    "skipNumber": 0,
    "included": [
      "title"
    ]
  },
  "_partialIncludes": false,
  "_pagingFunction": "pagingFunction(){return this.find(i)}",
  "_hasExtraItem": false,
  "nextSkipNumber": 0,
  "prevSkipNumber": -50
}

@tsuyoidesigns And if you add console logs for each variable it returns nothing?

Example:

let year = VINInfo.Results[9].Value;
console.log(year) 

@lmeyer When I console log each variable it returns as it should. However, when I run the query nothing is returned from the database. The code use to work with Corvid but since the switch to Velo I’ve been having trouble with it.

@tsuyoidesigns Have you tried running your query with just one of the variables to test? Like start with with this and see if it returns anything, then add the next variable, and so on.

wixData.query("battery2221")
.include("title")
.eq("year", year)
.find()

@lmeyer Yep, still no results.

@tsuyoidesigns Have you tried chaining the query to be sure things are executing in the correct order?

export function button1_click(event, $w) {

    let year;
    let make;
    let model;
    let engine;
    let car;

    getVINInfo($w("#vininput").value)
        .then(VINInfo => {
            console.log(VINInfo)

            year = VINInfo.Results[9].Value;
            make = VINInfo.Results[6].Value;
            model = VINInfo.Results[8].Value;
            engine = VINInfo.Results[71].Value + " " + "Liters";
            car = VINInfo.Results[9].Value + "  " + VINInfo.Results[6].Value + "  " + VINInfo.Results[8].Value;
        })
        .then(() => {
            wixData.query("battery2221")
                .include("title")
                .eq("year", year)
                .eq("make", make)
                .eq("model", model)
                .find()
                .then((results) => {
                    console.log(results);

                    $w("#results").text = "Vehicle Specific Battery for Your" + " " + car;
                })
        })
}

@lmeyer I copied the code you pasted and still no results. What could be the problem? The site is live and customers are calling in complaining about the site not working.

@tsuyoidesigns Not sure! Assuming you’ve already double-checked and triple-checked that the Database name and Field Key names and are all correct?

I do something similar on my site but structure it a bit differently when setting the variables, maybe give this a try. Sort of my last idea…

Maybe try changing this section

year = VINInfo.Results[9].Value;
make = VINInfo.Results[6].Value;
model = VINInfo.Results[8].Value;
engine = VINInfo.Results[71].Value +" "+"Liters";
car = VINInfo.Results[9].Value +"  "+ VINInfo.Results[6].Value +"  "+ VINInfo.Results[8].Value;

to this instead

year = VINInfo.items[0].year; // assuming "year" is your field key in the VIN database
make = VINInfo.items[0].make; // assuming "make" is your field key
model = VINInfo.items[0].model; // assuming "model" is your field key, etc
engine = VINInfo.items[0].engine + " " + "Liters";
car = VINInfo.items[0].make + "  " + VINInfo.items[0].model + "  " + VINInfo.items[0].year;

@lmeyer The VINInfo is a function thats called from the backend so changing that section wouldn’t work.

The only problem I’m having is the database query not producing the results even though the console log shows the following:


_query: 
{...}
jsonTableCopy JSON

orderBy: 
"undefined"

invalidArguments: 
[...]

filterTree: 
{...}
jsonTableCopy JSON

$and: 
Array(3)
jsonTableCopy JSON

0: 
{...}
jsonTableCopy JSON

year: 
"2019"

1: 
{...}
jsonTableCopy JSON

make: 
"HONDA"

2: 
{...}
jsonTableCopy JSON

model: 
"Civic"
{
  "$and": [
    {
      "year": "2019"
    },
    {
      "make": "HONDA"
    },
    {
      "model": "Civic"
    }
  ]
}

@yisrael-wix @shantanukumar847 Is there anything you can find wrong within my code by any chance?

@tsuyoidesigns Sorry I can’t be of more help! I do this same thing on my site with the code samples above (call info from the backend, pass it into variables, and perform a query with those variables) and it works perfectly. Not sure what the problem is but I hope you find a solution.

@lmeyer Thank you so much for your insight. Hopefully I can figure out what the issue soon.

Thanks for being awesome!!

Hello,

so if your problem is just the quering of the DB, what could be helpful ???

Than we do not need all that…

import { getVINInfo } from 'backend/VINModule';
import wixData from 'wix-data';

$w.onReady(function () {
  $w('#button1').onClick(async()=>{
    let yyy = $w("#vininput").value
    let VINInfo = await getVINInfo(yyy);    console.log(VINInfo)
    let year = VINInfo.Results[9].Value;    console.log(year);
    let make = VINInfo.Results[6].Value;    console.log(make);
    let model = VINInfo.Results[8].Value;   console.log(model);
    let engine = VINInfo.Results[71].Value + " " + "Liters"
    let car = VINInfo.Results[9].Value + "  " + VINInfo.Results[6].Value + "  " + VINInfo.Results[8].Value;

    wixData.query("battery2221")
    .include("title")
    .eq("year", year)
    .eq("make", make)
    .eq("model", model)
    .find()
    .then((results) => {console.log("RESULTS: ", results);
      $w("#results").text = "Vehicle Specific Battery for Your" + " " + car;
    });
});

All we need to find the issue would be some few steps…

  1. Testing the already existing DB for ERRORs.
  2. Testing on a new created DB with same CODE.
  3. If you still use sandboxed-DB → (Preview/Live-Mode) double-check that both DBs have same values. Perhaps at this point my suggestion would be even to deactivate the sandbox (i myself do not use it anymore).
  4. Using the most simple code for queriying your COLLECTION, without any additional functions inside…(TESTING)
import wixData from 'wix-data';

$w.onReady(function() {console.log("Page is ready!");
  $w('#button1').onClick(async()=>{console.log("Button clicked!")
    wixData.query("COLLECTION").find()
    .then((results)=>{console.log("RESULTS: ", results);})
    .catch((err)=>{console.log(err, "Something went wrong queriying DB!")});
  });
});

@russian-dima THANK YOU!!! This worked and I am now able to complete my database! I only wish there was an easier way for adding the reference field now. Clicking one by one on over 21 cells is very tedious but the outcome will be awesome lol

Thanks again for your help. Happy coding!

@tsuyoidesigns No problem.

I only wish there was an easier way for adding the reference field now. Clicking one by one on over 21 cells is very tedious but the outcome will be awesome.

What do you mean exactly?
How do look like your current code?
Mark the related code-parts to see what you describe…

@russian-dima Sorry, I am just seeing this. In reference to your question, I ended up figuring that part out on my own. Now I’m having trouble trying to create an if statement based on the results from the query you previously helped with. As an example, my Store/Products collection has an item called H6 (48), the query above gives the result of 48. I need to know how to create some code that will function to show something like this if (battery ===48) {battery = H6). Any suggestions?

BTW- I’ve pasted my code below for reference. I did try using .contains but it populates the incorrect item. Besides this, my code works flawlessly.

wixData.query("battery2221")
        .eq("year", year)
        .eq("make", make)
        .eq("model", model)
        .find()
        .then((results1) => {
            if (results1.totalCount > 1) {
              wixData.query("battery2221")
        .limit(1000)
        .eq("year", $w("#yearDropdown").value)
        .eq("model", $w("#modelDropdown").value)
        .eq("engine", $w("#engineDropdown").value)
        .find()        
        .then((res) => {
            let batt =  res.items[0].battery;
            console.log(res)
             wixData.query("Stores/Products")
                .contains("name", "Group Size " + batt)
                .find()
                .then((results2) => {
                    console.log(results2)
                    $w("#repeater1").show();
                    $w("#image8").src = results2.items[0].mainMedia;
                    $w("#text4").text = results2.items[0].name;
                    $w("#text6").html = results2.items[0].description;
                    $w("#text8").text = results2.items[0].formattedPrice;
                    $w("#goToProduct").onClick(() => {
                        wixLocation.to("https://www.24hourbattery.com/product-page/" + res.items[0].slug);
                     
        })}
                )

@tsuyoidesigns
Can you show a screenshot of the mentioned query-results from your console?
Please do not forget to open the results.

  1. You query your Stores/Products
  2. Searching for… .
contains("name","Group Size "+batt)
  1. You get results …
console.log(res) //<-- need this console-log (opened)
console.log(results2) //<-- need this console-log (opened)
  1. You get this one …

an item called H6 (48) ← is it a STRING? Is it the INDEX, is it a NUMBER? What is it?

  1. Did not really understand what you are trying to achieve…
if (battery === 48) {battery = H6}