1

I have recently come across SurrealDB, and installed it.

After adding to path, I started the server using surreal start --log trace --user root --pass root


 .d8888b.                                             888 8888888b.  888888b.
d88P  Y88b                                            888 888  'Y88b 888  '88b
Y88b.                                                 888 888    888 888  .88P
 'Y888b.   888  888 888d888 888d888  .d88b.   8888b.  888 888    888 8888888K.
    'Y88b. 888  888 888P'   888P'   d8P  Y8b     '88b 888 888    888 888  'Y88b
      '888 888  888 888     888     88888888 .d888888 888 888    888 888    888
Y88b  d88P Y88b 888 888     888     Y8b.     888  888 888 888  .d88P 888   d88P
 'Y8888P'   'Y88888 888     888      'Y8888  'Y888888 888 8888888P'  8888888P'


[2022-09-27 17:37:44] INFO  surrealdb::iam Root authentication is enabled
[2022-09-27 17:37:44] INFO  surrealdb::iam Root username is 'root'
[2022-09-27 17:37:44] INFO  surrealdb::dbs Database strict mode is disabled
[2022-09-27 17:37:44] INFO  surrealdb::kvs Starting kvs store in memory
[2022-09-27 17:37:44] INFO  surrealdb::kvs Started kvs store in memory
[2022-09-27 17:37:44] INFO  surrealdb::net Starting web server on 0.0.0.0:8000
[2022-09-27 17:37:44] INFO  surrealdb::net Started web server on 0.0.0.0:8000

In my NodeJS app, I have the following code (adapted from their docs):

import Surreal from 'surrealdb.js';
const db = new Surreal('http://localhost:8000/rpc');

async function main() {
    try {

        // Signin as a namespace, database, or root user
        await db.signin({
            user: 'root',
            pass: 'root',
            NS: 'practice',
            DB: 'buybig'
        });
        console.log('y');

        // Select a specific namespace / database
        // await db.use('practice', 'buybig');

        console.log(await db.select('users'));

    } catch (e) {
        console.error('ERROR', e);
    }
}

main();

I am getting this error:

ERROR AuthenticationError: There was a problem with authentication
    at Surreal._Surreal_signin (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:416:23)    at Surreal.<anonymous> (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:225:111)   
    at Surreal.f (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:28:18)     
    at file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:34:22
    at Array.forEach (<anonymous>)
    at Surreal.emit (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:33:67)  
    at Socket.<anonymous> (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:126:29)     
    at file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:34:22
    at Array.forEach (<anonymous>)
    at Socket.emit (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:33:67)   
^C

And this in my SurrealDB logs:

[2022-09-27 18:06:04] INFO  surreal::web 127.0.0.1:64675 GET /rpc HTTP/1.1 101 "-" 68.7µs

Accessing database through VSCode's Thunder Client and SurrealDB cli tool works flawlessly.

Any help is appreciated.

Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21

3 Answers3

4

I tried that as well and got the same error. The following worked for me:

let dataBase = await new Surreal();
await dataBase.connect("http://127.0.0.1:8000/rpc");
await dataBase.signin({
  user: "root",
  pass: "root",
});
await dataBase.use("test", "test");

let result = await dataBase.create("user:someone", {
  name: { first: "someone", last: "else" },
});
console.log(result);
user16217248
  • 3,119
  • 19
  • 19
  • 37
0

I had the same issue but it turns out the answer is a bit more complicated.

this only works for the root login

dataBase.signin({
  user: "root",
  pass: "root",
});

If you define a table named user and your user field is called email then your signin should look like this:

dataBase.signin({
  email: "get_email_from_post_params",
  pass: "get_pass_from_post_params",
});

Same thing goes for the pass field. As far as I can tell signin and signup must be called with an object whose keys match the columns in your table. Even though the argument of the methods is typed, it is broken. Also, I don't know if I'm doing something wrong, or its just because of my data structure, but it seems that signin and signup will throw an error if they're not passed SC, DB, and NS along with the user and the password. Basically your signup should look like this (don't forget to add a token on your scope)

dataBase.signup({
  email: "get_email_from_post_params",
  pass: "get_pass_from_post_params",
  SC: "your_scope",
  DB: "your database"
  NS: "your namespace"
});

I don't know if it's by design or just beta stuff, but the docs don't cover any of this and their examples are lacking. Took me the better part of a day to figure this out. Maybe it should be implied from the docs, it just wasn't for me. Like anybody playing with a brand new toy, I followed the instructions first.

Also, note that surreal doesn't magically issue JWT tokens. You need to explicitly declare a token on your scope.

man
  • 498
  • 5
  • 12
0
  1. This too doesn't seem to work . I went throught the surreal db websocket code this is how they structured the input,
let dataBase =  new Surreal();
    dataBase.connect("http://127.0.0.1:8000/rpc", {
     auth: {
       user: "root",
       pass: "root",
     },
     ns: "test",
     db: "test",
   });
  1. you can then use the database object in a async fn or use promises to fetch the data
async main(){
    const result = await dataBase.select("person");
    console.log(result);
}

Output :

[
  { id: 'person:22', name: 'likith' },
  { age: 22, bf: [ 'person:22' ], id: 'person:anas', name: 'anas' }
]