【发布时间】:2021-04-28 08:52:04
【问题描述】:
在下面的代码中,您看到它的作用是连接到 mongodb 数据库,并获取存储的用户 ID,并且工作正常,问题是向该用户 ID 发送消息,这是代码:
const { MongoClient, ReadPreference } = require('mongodb');
const MONGO_URL = 'mongodb://127.0.0.1:27017/';
(async() => {
const mongoClient = await MongoClient.connect(MONGO_URL, {
appname: 'test',
readPreference: ReadPreference.PRIMARY,
useNewUrlParser: true,
});
const db = await mongoClient.db('test');
const changeStream = db.collection('tools').watch([], { 'fullDocument': 'updateLookup' });
changeStream.on('change', (event) => {
tool.findOne({
code: datos,
}, async(err, s) => {
console.log(datos)
const userID2 = s.userID;
if (!s) {
console.log("pin no encontrado")
}
if (userID2 != undefined) {
try {
setTimeout(function() {
console.log("userID2 = " + userID2)
console.log("Event" = event);
console.log("event.updateDescription.updatedFields =" + event.updateDescription.updatedFields)
const user = client.users.cache.get(userID2)
console.log("const user = " + user)
user.send("Resultados del scan: " + "\nEstá el usuario cheat? " + event.updateDescription.updatedFields.cheats + "\nCheats que tiene el usuario: " + event.updateDescription.updatedFields.namecheats)
}), 5000
} catch (e) { console.log("[ERROR]", e) }
} else { return }
});
})
})();
})
这是控制台的输出:
651312
userID2 = 794601739960844348
{
_id: {
_data: '82600D5062000000012B022C0100296E5A1004D5046A26D5AE482380F594D10FDC475546645F69640064600CA9A0FE28E8160C78C34E0004'
},
operationType: 'update',
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1611485282 },
fullDocument: {
_id: 600ca9a0fe28e8160c78c34e,
serverID: '802595009617723412',
userID: '794601739960844348',
__v: 0,
cheats: 'true',
code: '651312'
},
ns: { db: 'test', coll: 'tools' },
documentKey: { _id: 600ca9a0fe28e8160c78c34e },
updateDescription: { updatedFields: { cheats: 'true' }, removedFields: [] }
}
{ cheats: 'true' }
const user = undefined
/home/jonh/bot-tool/src/index.js:101
user.send("Resultados del scan: " + "\nEstá el usuario cheat? " + event.updateDescription.updatedFields.cheats + "\nCheats que tiene el usuario: " + event.updateDescription.updatedFields.namecheats)
^
TypeError: Cannot read property 'send' of undefined
at Timeout._onTimeout (/home/jonh/bot-tool/src/index.js:101:34)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
npm ERR! code 1
npm ERR! path /home/jonh/bot-tool
npm ERR! command failed
npm ERR! command sh -c node .
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-01-24T10_48_02_631Z-debug.log
如您所见,在输出中,它记录了 userID2 = 794601739960844348 并且用户 ID 很好,但由于某种原因它没有向该用户发送消息。 我已经用 fetch() 试过了,但是没用。
【问题讨论】:
标签: mongodb discord.js