英文:
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');
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论