MagicJS
mern.aiUniversityGitHub
  • Introduction to MagicJS
  • Why MagicJS?
  • Getting Started & Installation
  • Basic Guide
    • Create a new page using React
    • Navigate between pages
    • Create an API and integrate it with the frontend
    • Authenticate Users
      • Authorise based on Roles
    • Advanced State Management with useContent()
    • Perform CRUD Operations
    • Adding Realtime capabilities using socket
    • Handling file uploads and downloads
  • Advanced Guide
    • Understanding the concept of features in MagicJS
    • Using UI components & functions across multiple Magic Features
    • Advanced Routing of pages
    • Enable SSR
    • Access MongoDB
    • Styling pages using Tailwind CSS
  • Deploying
  • Update MagicJS
  • API References
    • Frontend
      • <LinkDisplay>
      • createSrc()
      • createUploader()
      • importUI()
      • loadConfig()
      • protected()
      • useParams()
      • useAxios()
      • useLogin()
      • useSocket()
      • useContent()
      • usePromise()
      • useNotification()
    • Backend
      • createBackendFunction()
      • data()
      • io()
      • ServerInstance()
      • utils
        • hash()
        • verifyHash()
        • initiateEmailVerification()
        • saveFileToUserUploads()
        • readFileFromUserUploads()
        • removeFileFromUserUploads()
        • assignRoleToUser()
        • unassignRoleFromUser()
        • findAllRolesByUser()
        • isUserInAnyRoles()
        • assignRoleToUser()
Powered by GitBook
On this page

Was this helpful?

  1. API References
  2. Backend

data()

The data function serves as a crucial component within the system, playing a pivotal role in the execution of various data operations. Leveraging this function, developers can seamlessly interact with and manipulate data stored in the backend, facilitating tasks such as retrieving, inserting, updating, or deleting records within designated data collections.

Example:

import { createBackendFunction, data } from "@magicjs.dev/backend";
import { ObjectId } from 'mongodb';

export default createBackendFunction(async function (id) {
    try {
        const itemCollection = data('mongoDbCollectionName');

        // Pass the MongoDB collection name as an argument to the data function,
        // allowing you to retrieve the corresponding data collection.
        const itemsDetails = await itemCollection.findOne({
            _id: new ObjectId(id)
        });

        return itemsDetails;
    } catch (error) {
        throw new Error(`Network error`);
    }
});
PreviouscreateBackendFunction() Nextio()

Last updated 1 year ago

Was this helpful?

Click here to refer GitHub.