openvidu-tutorials icon indicating copy to clipboard operation
openvidu-tutorials copied to clipboard

Gettiing Error: [Object object] on OV.createSession

Open AliYusufzai opened this issue 1 year ago • 0 comments

what am I doing wrong in this code that im getting this error, before i was getting it because of not running the docker now i have tried to go mvc way and this error is coming. before i was running the file server.js

` // controllers/sessionsController.js const { OpenVidu, OpenViduRole } = require("openvidu-node-client"); const User = require("../model/user"); const jwt = require("jsonwebtoken");

// Get the OpenVidu URL and secret from environment variables const OPENVIDU_URL = process.env.OPENVIDU_URL || "http://localhost:4443"; const OPENVIDU_SECRET = process.env.OPENVIDU_SECRET || "MY_SECRET";

// Entrypoint to OpenVidu Node Client SDK const OV = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);

const getToken = async (req, res) => { const accessToken = req.cookies.jwt; if (!accessToken) { return res.status(401).json({ message: "User not logged in" }); } try { const decodeToken = jwt.verify(accessToken, process.env.JWT_SEC); // console.log("Decoded Token:", decodeToken); const user = await User.findOne({ _id: decodeToken.userId }); if (!user) { return res.status(404).send("User not found in the database"); }

    // Collection to pair session names with OpenVidu Session objects
    var mapSessions = {};
    // Collection to pair session names with tokens
    var mapSessionNamesTokens = {};

    const sessionName = req.body.sessionName;
    const role = OpenViduRole.PUBLISHER;
    const serverData = JSON.stringify({ serverData: user.username });

    const connectionProperties = {
        data: serverData,
        role: role,
    };

    if (mapSessions[sessionName]) {
        // Existing session
        console.log("Existing session " + sessionName);
        // Get the existing Session from the collection
        var mySession = mapSessions[sessionName];

        // Generate a new token asynchronously with the recently created connectionProperties
        try {
            const connection = await mySession.createConnection(
                connectionProperties
            );
            // Store the new token in the collection of tokens
            mapSessionNamesTokens[sessionName].push(connection.token);
            // Return the token to the client
            res.status(200).send({
                0: connection.token,
            });
        } catch (error) {
            console.error(error);
            res.status(500).send("Internal Server Error");
        }
    } else {
        // New session
        console.log("New session " + sessionName);

        // Create a new OpenVidu Session asynchronously
        try {
            const session = await OV.createSession();
            // Store the new Session in the collection of Sessions
            mapSessions[sessionName] = session;
            // Store a new empty array in the collection of tokens
            mapSessionNamesTokens[sessionName] = [];

            // Generate a new connection asynchronously with the recently created connectionProperties
            const connection = await session.createConnection(
                connectionProperties
            );
            // Store the new token in the collection of tokens
            mapSessionNamesTokens[sessionName].push(connection.token);
            // Return the Token to the client
            res.status(200).send({
                0: connection.token,
            });
        } catch (error) {
            console.error(error);
            res.status(500).send("Internal Server Error");
        }
    }
} catch (err) {
    console.error(err);
    res.status(401).send("Invalid token");
}

};

module.exports = { getToken }; `

AliYusufzai avatar Jul 27 '23 12:07 AliYusufzai