Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongoose(JavaScript)中的文档问题_Javascript_Node.js_Database_Mongodb_Mongoose - Fatal编程技术网

Mongoose(JavaScript)中的文档问题

Mongoose(JavaScript)中的文档问题,javascript,node.js,database,mongodb,mongoose,Javascript,Node.js,Database,Mongodb,Mongoose,所以我制作了一个机器人来解决不和谐,但我和猫鼬有一些问题。所以我想要的基本上是,用户发送一条消息,用他的一些信息保存一个文档,但是如果已经有一个文档包含他的信息,它将停止这个过程,并返回。所以我试了一下: function main(message){ // So first user sends a message to store some data about him let author = message.author //this is d

所以我制作了一个机器人来解决不和谐,但我和猫鼬有一些问题。所以我想要的基本上是,用户发送一条消息,用他的一些信息保存一个文档,但是如果已经有一个文档包含他的信息,它将停止这个过程,并返回。所以我试了一下:

      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语句中失败,该语句检查用户是否已有包含其信息的文档。我尝试过多种方法,但都不起作用。 我认为这个问题的解决方案与异步函数有关,但我不确定,而且我对异步进程也不太了解


提前谢谢

问题在于,您将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包装在数组中?我不知道你的模式,但它似乎很奇怪,可能会导致你的问题<代码>用户id:[id]

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


关于包装id,实际上它只能这样工作,如果我不包装它,它将无法识别变量。