【发布时间】:2020-12-26 02:28:36
【问题描述】:
所以我正在制作一个不和谐的机器人,但我遇到了 Mongoose 的一些问题。所以我想要的基本上是,用户发送一条消息来保存一个包含他的一些信息的文档,但是如果已经有一个包含他的信息的文档,它将停止该过程并返回。所以我尝试了这个:
function main(message){
// So first user sends a message to store some data about him
let author = message.author //this is discord.js syntax, basically it returns the author of a message
let id = author.id //also discord.js syntax, returns the id from the user, in this case the author variable above
let check = logUser.findOne({userId : [id]}).exec().then(res => {
if (res) return true;
else return false;
})} // So if there is a Document with the id of the author of the message it will return true, else it returns false
if (check === true) return console.log("This User has already a Document with his info saved");
//so if the user has already a Document with his info it will return and stop the action of saving his Data
//everything from this point is basic Mongoose Syntax, to make a Document with User data
const theUser = new logUser({
_id : mongoose.Types.ObjectId(),
userName : author.username,
userId : author.id,
currency : 0
})
theUser.save()
.then(result => console.log(result))
.catch(err => console.log(err))
console.log(`User ${author.username} was stored into the database!`)
}
它在检查用户是否已经拥有包含其信息的文档的 if 语句中失败。我已经尝试了多种方法,但它不起作用。 我认为这个问题的解决方案与异步函数有关,但我不确定,而且我对异步进程了解不多。
提前致谢!
【问题讨论】:
标签: javascript node.js database mongodb mongoose