【问题标题】:Issues with the Documents in Mongoose (JavaScript)Mongoose (JavaScript) 中的文档问题
【发布时间】: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


    【解决方案1】:

    问题在于您将 logUser.findOne 视为同步的。像这样在 findOne 回调中执行检查:

      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
        
        logUser.findOne({userId : [id]}).exec().then(res => {
          let check = Boolean(res);
          if (check === true)
            return console.log("This User has already a Document with his info saved");
    
          const theUser = new logUser({
            _id : mongoose.Types.ObjectId(),
            userName : author.username,
            userId : author.id,
            currency : 0
          });
    
          theUser.save()
            .then(result => {
              console.log(result);
              console.log(`User ${author.username} was stored into the database!`)
            })
            .catch(err => console.log(err))
        });
    }
    

    您是否故意将 id 包装在一个数组中?我不知道您的架构,但这似乎很奇怪,可能会导致您的问题。 userId : [id]

    您可能需要考虑使用 async/await 来减少回调。您还可以考虑使用唯一索引来避免将来出现多个请求。尝试保存同一个文档两次时,使用唯一索引会引发错误。

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await https://docs.mongodb.com/manual/core/index-unique/

    【讨论】:

    • 关于 id 的封装,其实只能这样,不封装就无法识别变量。
    猜你喜欢
    • 2019-05-24
    • 2017-05-29
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2013-01-14
    • 2016-09-13
    • 1970-01-01
    • 2022-07-12
    相关资源
    最近更新 更多