我正在尝试在Pusher的Chatkit SDK中创建一个userId和userName。我能够创建唯一的用户ID,但是由于某些原因,“名称”变量未传递到chatkit / user /端点。
谢谢!
下面是我的代码:
前端离子/角度代码
addUser(user_id: string, first_name: string, last_name: string) {
let userId = `${user_id.toString()}`;
let userName = `${first_name.toString()}`;
console.log (userId, userName);
axios.post('http://localhost:7000/users', { id: userId, name: userName } )
.then(() => {
const tokenProvider = new Chatkit.TokenProvider({
url: 'http://localhost:7000/authenticate'
});
const chatManager = new Chatkit.ChatManager({
instanceLocator: 'INSTANCE_LOCATER_ID',
userId,
tokenProvider
});
return chatManager
.connect()
.then(currentUser2 => {
this.currentUser2 = currentUser2;
});
})
.catch(error => console.error(error))
}
后端代码Node Express JS (http://localhost:7000)
require('dotenv').config({ path: '.env' });
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const Chatkit = require('@pusher/chatkit-server');
const app = express();
const chatkit = new Chatkit.default({
instanceLocator: process.env.CHATKIT_INSTANCE_LOCATOR,
key: process.env.CHATKIT_SECRET_KEY,
});
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/users', (req, res) => {
const { userId, userName } = req.body;
chatkit
.createUser({
id: userId,
name: userName,
})
.then(() => {
res.sendStatus(201);
})
.catch(err => {
if (err.error === 'services/chatkit/user_already_exists') {
console.log(`User already exists: ${userId}`);
res.sendStatus(200);
} else {
res.status(err.status).json(err);
}
});
});
app.post('/authenticate', (req, res) => {
const authData = chatkit.authenticate({
userId: req.query.user_id,
});
res.status(authData.status).send(authData.body);
});
app.set('port', process.env.PORT || 5200);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});