Wix Data get() not working

I’m trying to use the get() function to retrieve data for a particular item in a database using its ID. I call .then() immediately after to resolve the returned promise. This isn’t working, and instead of the item, I get back:

{
“isFulfilled”: false,
“isRejected”: false
}

I have all the right permissions to get data from this database; what could be causing this?

Need more information in order to help. What is your code? Collection?

Here is the code:

let entry = wixData.get(“clickCounter”, “ID number”)
.then( (results) => {
let item = results; //see item below
} )
. catch ( (err) => {
let errorMsg = err;
} );

‘clickCounter’ is the name of the collection; ‘entry’ evaluates to the object in the previous comment with this code.

@pbass450 The data for the retreived item is results returned in the .then() segment. To see for yourself, add a console.log() statement like this:

let item = results; //see item below
console.log(item);

The console.log() statement will display all of the data contained in the returned item.

For more information, see the .get() API.

@yisrael-wix I’m using this function for an HTTP request handler and I’m returning ‘item’ in the body of the response, and it’s still showing up as a promise. Where can I see the console when editing backend code?

@yisrael-wix Actually now it doesn’t look like it’s returning anything when I set the body of the response to item, there was a line later on that reset the response body but commenting that out doesn’t return anything at all.

@yisrael-wix I copied that snippet into the code for a page and logged item to the console, and it was exactly what it should be. Is there a reason it’s not working in the HTTP functions file?

@pbass450 Hmm, I know the mechanism works since I have a myApi example that demonstrates both the client (fetch) and the server (http functions). You can play with that example to see how the mechanism works.

See the HTTP functions debugging for info. You can also use Site Monitoring for a more robust (but more complex) way to debug.

A simpler way to debug in the backend (and the http functions) is by “logging” debug statements to a database collection. Instead of using a console.log() , create a collection called log , and .insert() records, with the message, to the collection.

@yisrael-wix Thanks, I was able to retrieve the item using await in a helper async function like was done in the example.

@yisrael-wix I’ve tried using the item in the backend code; I want to get an integer from the collection and increment it by one then update the entry in the collection with the new value, but it only seems to run every other time I make a request to the site. This isn’t a timing issue, waiting for periods between requests doesn’t affect behavior, but instead the first request doesn’t do anything and the second request increments the value. What could be happening?

@yisrael-wix I’m still working on this, and for some reason the promise returned by wixData.get() won’t resolve without using a separate async function, and then it only works every other time a request is made. Why would the promise not be resolving?

Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor. Please include the name of the page involved.

https://pbass450.wixsite.com/mysite There’s only one page on this site.

I see a number of issues:

In your page (frontend) code, you need to move your code into the page’s onReady() function. Also, you should also have an onReady() function for the dataset to ensure that it is ready before trying to use it. The wixData.get() doesn’t do anything and isn’t needed. Your page code should look something like this:

$w.onReady(function () {
    setInterval(() => $w('#dataset1').refresh(), 500);
});

In your backend code, you are not handling the Promises correctly. I would highly recommend learning more about Promises before continuing. For information on what Promises do and how to properly use them, see the following articles:

  • Corvid: Working with Promises

  • Promises, Promises

As an example, you have the following segment of code:

wixData.get("clickCounter", "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7")
    .then((results) => {
        newVal = results.value + 1; //see item below
    });

let toUpdate = {
    "_id": "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7",
    "title": "clicks",
    "value": newVal
};

The problem with the above code is that toUpdate gets executed before the .get() returns - newVal is not yet set with the retrieved value.

You would need something like this:

wixData.get("clickCounter", "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7")
    .then((results) => {
        newVal = results.value + 1;

         // you can newVal here
        let toUpdate = {
            "_id": "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7",
            "title": "clicks",
            "value": newVal
         };
    });


Another way (which might be easier to read and implement) would be to use await:

let results = await wixData.get("clickCounter", "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7");
newVal = results.value + 1;
let toUpdate = {
    "_id": "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7",
    "title": "clicks",
    "value": newVal
};

You will also need to correctly handle the Promise for the .update().

Good luck.

Thanks, putting the .update() in the argument of .then() fixed it.

Great! I can’t believe I actually got something done so early in the morning.

I’m having the same problem, query() is working but when I change to get() its not working…