当我运行代码时,它显示"用户已成功注册",但后跟"无法阅读属性'电子邮件'未定义"。 我尝试修复错误,但失败了。我认为在userRecord中可能有些错误,但我检查了很多次,无法找到问题所在。
这是我的代码:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var admin = require("firebase-admin");
var userAccountRequests = (io) =>{
io.on('connection',(socket)=>{
console.log(`Client ${socket.id} is connected`);
detectDisconnection(socket,io);
registerUser(socket,io);
});
};
我认为这里可能存在一些错误。它可以显示"成功"消息,但我想我已经定义了电子邮件。
function registerUser(socket,io){
socket.on('userData',(data)=>{
admin.auth().createUser({
email:data.email,
displayname:data.userName,
password:data.password
})
.then((userRecord)=>{
console.log('User was registered successfully');
var db = admin.database();
var ref = db.ref('users');
var userRef = ref.child(encodeEmail(data.email));
var data = {
data:admin.database.ServerValue.TIMESTAMP
};
userRef.ser({
email:data.email,
userName:data.userName,
dataJoined:date,
hasLoggedIn:false
});
//send message to Client
Object.keys(io.sockets.sockets).forEach((id)=>{
if(id == socket.id){
var message = {
text:'Success'
}
io.to(id).emit('message',{message});
}
});
}).catch((error)=>{
Object.keys(io.sockets.sockets).forEach((id)=>{
console.log(error.message);
if(id == socket.id){
var message = {
text:error.message
}
io.to(id).emit('message',{message});
}
});
});
});
}
function detectDisconnection(socket,io){
socket.on('disconnect',()=>{
console.log('Client has disconnected');
});
}
function encodeEmail(email) {
return email.replace('.',',');
}
module.exports = {
userAccountRequests
}
答案 0 :(得分:0)
'数据'对象仅在' on'中的函数内有效,此代码:
socket.on('userData',(data)=>{
admin.auth().createUser({
email:data.email,
displayname:data.userName,
password:data.password
})
在此功能结束时,'数据'不再定义。然后'然后'以下是使用' userRecord'代替。
您应该更改您的数据'引用' userRecord',如下所示:
function registerUser(socket,io){
socket.on('userData',(data)=>{
admin.auth().createUser({
email:data.email,
displayname:data.userName,
password:data.password
})
.then((userRecord)=>{
console.log('User was registered successfully');
var db = admin.database();
var ref = db.ref('users');
var userRef = ref.child(encodeEmail(userRecord.email));
// not sure what this is about
var data = {
data:admin.database.ServerValue.TIMESTAMP
};
userRef.ser({
email:userRecord.email,
userName:userRecord.userName,
dataJoined:date,
hasLoggedIn:false
});
//send message to Client
Object.keys(io.sockets.sockets).forEach((id)=>{
if(id == socket.id){
var message = {
text:'Success'
}
io.to(id).emit('message',{message});
}
});
}).catch((error)=>{