它无法接收对象ID

huangapple go评论79阅读模式
英文:

It can't receive Object ID

问题

I have a problem with showing Object ID from MongoDB in the console in VS Code. I want to get ObjectID for the user, who is log in in the my application and I create input tag, which is hidden and with id = ObjectID.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Car Rental</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="/style.css">
    <script src="https://code.jquery.com/jquery-3.6.4.js" integrity="sha256-a9jBBRygX1Bh5lt8GZjXDzyOB+bWve9EiO7tROUtj/E=" crossorigin="anonymous"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script src="/script.js"></script>
</head>
<body>
    {{> navbar}}
    {{{body}}}
    <input type="hidden" id="ObjectID" value="{{user._id}}">
</body>
</html>

When I run the apllication and log in with user, in the console in the browser, I see that it gets ObjectId for current User.

Then I make a little code for this ObjectID in the file, where i connect socket.io. There I use '#ObjectID' to find this ObjectID.

// connect Socket.io

$(document).ready(function(){
    var socket = io();

    // connect client to server
    socket.on('connect', function(socket){
        console.log('Connected to server');
    });

    // emit user id
    var ObjectID = $('#ObjectID')
    socket.emit('ObjectID', ObjectID);

    // disconnect from server
    socket.on('disconnect', function(socket){
        console.log('Disconnected from server');
    });
});

After that, I want to create Object ID event in server file "app.js" and I want to see this ObjectId in the console of Node.js.

// socket connection
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection',(socket)=>{
    console.log('Connected to client');
    // listen to object ID event
    socket.on('ObjectIO',(ObjectID)=>{
        console.log('User ID is: ', ObjectID);
    });

    // listen to disconnection
    socket.on('disconnect',(socket)=>{
        console.log('Disconnected from Client');
    });
});

But after run the application and after log in a user, I don't see in the console to print current ObjectID.

Please help me to fix this problem.

英文:

I have a problem with showing Object ID from MongoDB in the console in VS Code. I want to get ObjectID for the user , who is log in in the my application and I create input tag , which is hidden and with id = ObjectID .

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Car Rental</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="/style.css">
    <script src="https://code.jquery.com/jquery-3.6.4.js" integrity="sha256-a9jBBRygX1Bh5lt8GZjXDzyOB+bWve9EiO7tROUtj/E=" crossorigin="anonymous"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script src="/script.js"></script>
</head>
<body>
    {{> navbar}}
    {{{body}}}
    <input type="hidden" id="ObjectID" value="{{user._id}}">
</body>
</html>

When I run the apllication and log in with user, in the console in the browser , I see that it gets ObjectId for current User.

Then I make a little code for this ObjectID in the file, where i connect socket.io. There I use '#ObjectID' to find this ObjectID.

// connect Socket.io

$(document).ready(function(){
    var socket = io();

    // connect client to server
    socket.on('connect', function(socket){
        console.log('Connected to server');
    });

    // emit user id
    var ObjectID = $('#ObjectID')
    socket.emit('ObjectID', ObjectID);

    // disconnect from server
    socket.on('disconnect', function(socket){
        console.log('Disconnected from server');
    });
});

After that , I want to create Object ID event in server file "app.js" and I want to see this ObjectId in the console of Node.js .

// socket connection
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection',(socket)=>{
    console.log('Connected to client');
    // listen to object ID event
    socket.on('ObjectIO',(ObjectID)=>{
        console.log('User ID is: ', ObjectID);
    });

    // listen to disconnection
    socket.on('disconnect',(socket)=>{
        console.log('Disconnected from Client');
    });
});

But after run the application and after log in a user , I don't see in the console to print current ObjectID .

Please help me to fix this problem.

答案1

得分: 0

You are listening for ObjectIO and you are emitting ObjectID. Change socket.on('ObjectIO',(ObjectID)=>{ to socket.on('ObjectID',(ObjectID)=>{

英文:

You are listening for ObjectIO and you are emitting ObjectID. Change socket.on('ObjectIO',(ObjectID)=>{ to socket.on('ObjectID',(ObjectID)=>{

// socket connection
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection',(socket)=>{
    console.log('Connected to client');
    // listen to object ID event
    socket.on('ObjectID',(ObjectID)=>{
        console.log('User ID is: ', ObjectID);
    });

    // listen to disconnection
    socket.on('disconnect',(socket)=>{
        console.log('Disconnected from Client');
    });
});

huangapple
  • 本文由 发表于 2023年4月17日 18:55:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定