Giter Site home page Giter Site logo

remult Next.js Tutorial about remult HOT 8 CLOSED

remult avatar remult commented on May 22, 2024
remult Next.js Tutorial

from remult.

Comments (8)

noam-honig avatar noam-honig commented on May 22, 2024 1

@quangtynu, thanks for letting me know that it works.

Let me try and clarify.

When using a BackendMethod you can define an optional parameter of type Remult and the BackendMethod will automatically inject the correct remult object in the context of the incoming request.

Actually - before version 0.16, this was the only way to use remult on a backend method.

In version 0.16 we introduced the ability to just use the remult object directly without defining the optional parameter - we did it by using the async_hooks feature of node.

Since not all javascript environment support async_hooks (for example, demo) - we made it optional.

In your case - in the next tutorial - the instructions at this time do not include an explanation on how to enable that feature that depends on async_hooks.

So - while we fix the tutorial, I wanted to give you a quick workaround.

To summarize - for now (until we fix the tutorial), when you use backend methods, add an optional parameter of type Remult to gain access to the remult object.

Once we update the tutorial, I'll let you know.

Sounds good?

from remult.

quangtynu avatar quangtynu commented on May 22, 2024 1

Thank you. @noam-honig . It works now.

from remult.

noam-honig avatar noam-honig commented on May 22, 2024

Hi @quangtynu,

Great catch - I see the issue and am working on it.

In the meantime, let me suggest a workaround, please add an optional parameter called remult of type Remult to the setAll method. (, remult?:Remult)
And adjust the code that defined the repo by adding a '!' before the .repo. should be remult!.repo(Task)

You don't need to send that parameter when you call it - the BackendMethod decorator'll automatically inject it.

So your code should look like this:

import { BackendMethod, Remult } from "remult";
import { Task } from "./Task";

export class TasksController {
  @BackendMethod({ allowed: true })
  static async setAll(completed: boolean, remult?: Remult) {
    const taskRepo = remult!.repo(Task);
    for (const task of await taskRepo.find()) {
      await taskRepo.save({ ...task, completed });
    }
  }
}

You can see a working example at a code sandbox I've created at:
https://codesandbox.io/p/sandbox/interesting-feistel-poit6u?file=%2Fsrc%2Fshared%2FTasksController.ts

Of course, I'm still working on the original issue you reported.

Please let me know that the workaround also works for you

from remult.

quangtynu avatar quangtynu commented on May 22, 2024

Ok, I found out:
modified code in file "src/shared/TasksController.ts" 👍
Origin code as described in:
https://remult.dev/tutorials/react-next/backend-methods.html#refactor-from-front-end-to-back-end

import { BackendMethod, remult } from "remult";
import { Task } from "./Task";

export class TasksController {
@BackendMethod({ allowed: true })
static async setAll(completed: boolean) {
const taskRepo = remult.repo(Task);

  for (const task of await taskRepo.find()) {
     await taskRepo.save({ ...task, completed });
  }

}
}
//---------------------------------
to this:

import { BackendMethod, Remult } from "remult";
import { Task } from "./Task";

export class TasksController {
@BackendMethod({ allowed: true })
static async setAll(completed: boolean, remult?: Remult) {
const taskRepo = remult!.repo(Task);

  for (const task of await taskRepo.find()) {
     await taskRepo.save({ ...task, completed });
  }

}
}

//---------------------------------
as described in:
https://remult.dev/docs/backendMethods.html#static-backend-method
it works now. But i dont interstand.

from remult.

quangtynu avatar quangtynu commented on May 22, 2024

Thank you for Your explain. it nice for me now. actually I also try to find into how its works.

from remult.

quangtynu avatar quangtynu commented on May 22, 2024

Now i come to: "Frontend setup" of "Authentication and Authorization" part:
https://remult.dev/tutorials/react-next/auth.html#user-authentication

the error in "pages/index.tsx" is on the browser:

Server Error
Error: [next-auth]: useSession must be wrapped in a

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
pages/index.tsx (19:38) @ Home

17 |
18 | const Home: NextPage = () => {

19 | const { data: session } = useSession();
| ^
20 |
21 | const [tasks, setTasks] = useState<Task[]>([]);
22 | const [hideCompleted, setHideCompleted] = useState(false);
Show collapsed frames

from remult.

yoni-rapoport avatar yoni-rapoport commented on May 22, 2024

It seems that in the current version of next-auth you have to add a <SessionProvider> to the _app.tsx for useSession() to work.

https://next-auth.js.org/getting-started/client#sessionprovider

We need to fix the tutorial to reflect that. Thanks!

from remult.

noam-honig avatar noam-honig commented on May 22, 2024

@quangtynu, I've fixed the original "async_hooks" error you've got and released a new version for it:
https://github.com/remult/remult/releases/tag/v0.17.0

If you can try it out and let me know - it'll be great.

Thanks

from remult.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.