surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
271 stars 46 forks source link

Bug: Does not save all the values. #219

Open dikovmaxim opened 1 year ago

dikovmaxim commented 1 year ago

Describe the bug

Hi. I am pretty new to Surreal, so excuse me if I'm asking a dumb question, but I wasnt abe to find enough Information in docs. When I am creating a user, and then requesting it using Typescript and surrealdb.js, the user is created and is can be queried just normal: image

But when I restart the application and query it again, this is the object I get: image

Maybe it has something to do with the way Ts, or better to say Js Runtime is sting the data, but I have no Idea, why such a bug happens. Here is the code to create a user: image

And here is the code I am sing to query it: image

Maybe I kinda misunderstood the Surrealdb SQL syntax. Before using this code I covered it with tests and this is why I was sure everything should work just fine.

P.S. I also tried Select * ...

Here are the code snippets in case you want to copy them: ` try { const query = " SELECT username, email, surname, lastname, type, token, logged_in, class, timetable, password FROM type::table($tb) WHERE username = $username";

            const params = {
              tb: 'users',
              username: username,
            };

            const result = await db.query(query, params);

            if (result.length < 0 || result[0].result.length < 1) {
              return null;
            }

            return result[0].result[0];
          } catch (error) {
            console.error('Error retrieving user:', error);
            throw error;
          }`

let created = await db.create("users:"+username,{ username: username, password: password, email: email, surname: surname, lastname: lastname, type: type, token: '', logged_in: false, class: null, timetable: null });

Steps to reproduce

Execute the SELECT statement

Expected behaviour

It always should return the image

SurrealDB version

1.0.0-beta.9+20230402.5eafebd for windows on x86_64

Contact Details

dikovmax04@gmail.com.

Is there an existing issue for this?

Code of Conduct

finnbear commented 1 year ago

Hello, thanks for opening this issue!

I noticed that, while most fields become null after you restart your application, the logged_in field goes from false to true. I don't see where this is possible in the code you provide, so I suggest:

Finally, it would be interesting to see any DEFINE TABLE or DEFINE FIELD statements you are using (if applicable).

kearfy commented 1 year ago

In addition to @finnbear comment, could you get a list of field definitions with INFO FOR TABLE users?

dikovmaxim commented 1 year ago

Thank you for your response: This is the Select * from users output: [{"time":"7.8992ms","status":"OK","result":[{"id":"users:dikoma15","logged_in":true,"username":"myusername"}]}] And this is the output for INFO FOR TABLE users; [{"time":"3.2342ms","status":"OK","result":{"ev":{},"fd":{},"ft":{},"ix":{}}}]

kearfy commented 1 year ago

Alrighty, raises some more questions for me:

dikovmaxim commented 1 year ago

Hi again. I checked everything and did multiple debugs agan. The problem is not in database but in api. You see, this code:

const query = "SELECT username, email, surname, lastname, type, token, logged_in, class, timetable, password FROM users WHERE username = "myusername";";

const params = {
 //username: username,
};

const result = await db.query(query, params);

works just fine, but when i try to use prepared statement, like:

const query = "SELECT username, email, surname, lastname, type, token, logged_in, class, timetable, password FROM users WHERE username = "$username";";

                const params = {
                  username: username,
                };

                const result = await db.query(query, params);

or

const query = "SELECT username, email, surname, lastname, type, token, logged_in, class, timetable, password FROM users WHERE username = $username;";

                const params = {
                  username: username,
                };

                const result = await db.query(query, params);

, it stops working

dikovmaxim commented 1 year ago

I continued debugging, and after I ran this code here, an error occured again: image result: image

Maybe I am doing something wrong. I really like SurrealDB, but such bugs are driving me nuts...

sgirones commented 4 months ago

@kearfy could you take a look?

oskar-gmerek commented 3 months ago

@dikovmaxim

what will happen if you remove:

if (result.length < 0 || result[0].result.length < 1) {
    return null;
}

and change:

return result[0].result[0];

to

return result[0][0]

It's just a guess from my side as its hard to reproduce your problem, as you provide only part of your code. But I'm pretty sure that is problem how you accessing response data.

If the above will not help, just try to find a minimal code that will result with your problem. For example, start with:

Check what is returned from SurrealDB, (do not wrap it in any extra functions):

const query = 'SELECT username, email, surname, lastname, type, token, logged_in, class, timetable, password FROM users WHERE username = $username;';

const params = {
    username: 'test' // make sure that user with username = 'test' exists in database
};

const test = async () => {
    try {
        const result = await db.query(query, params);
        console.log({ response: result[0][0] });
    } catch (error) {
        console.error('error:', error);
    }
};

Then check what is logged out. Is it error or result? Is it an array or object?

I think that kind of the problem can be as well a mismatch with the permissions on table or authentication itself or even typo on table name (is it users? or user?). Let us know what is returned from the above code and then will be much easier to determine where the problem is.

oskar-gmerek commented 3 months ago

I think the problem here was the example in docs not updated to v0.11.0 changes. @kearfy this little change should prevent similar problems (if I am right with guessing): surrealdb/docs.surrealdb.com#451