Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Does not save all the values. #219

Open
2 tasks done
dikovmaxim opened this issue Jun 4, 2023 · 9 comments
Open
2 tasks done

Bug: Does not save all the values. #219

dikovmaxim opened this issue Jun 4, 2023 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@dikovmaxim
Copy link

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?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@dikovmaxim dikovmaxim added the bug Something isn't working label Jun 4, 2023
@finnbear
Copy link

finnbear commented Jun 5, 2023

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:

  • look for places where you set logged_in to true and make sure they don't affect the other fields
  • make sure you are querying the exact same user record before and after restarting
  • using surreal sql to inspect the contents of the database to see whether the database or your application has an issue

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

@kearfy
Copy link
Member

kearfy commented Jun 5, 2023

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

@dikovmaxim
Copy link
Author

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
Copy link
Member

kearfy commented Jun 5, 2023

Alrighty, raises some more questions for me:

  • Have you defined the table schemafull?
  • Are you using some sort of schema validation library like zod?

@dikovmaxim
Copy link
Author

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
Copy link
Author

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
Copy link
Member

@kearfy could you take a look?

@tobiemh tobiemh transferred this issue from surrealdb/surrealdb Mar 29, 2024
@oskar-gmerek
Copy link

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants