简体   繁体   English

为什么返回整个文档时出错

[英]Why Is An Error Returning A Whole Document

I am trying to get a document using findOne , I have a simple error handler ( if (err) console.log(err); ), but is returning a whole document. 我正在尝试使用findOne获取文档,我有一个简单的错误处理程序( if (err) console.log(err); ),但是正在返回整个文档。 I can't get past this if statement. 我无法超越这个if陈述。 If I remove the if (err) , then it gets caught by another statement which says the document does not exist. 如果我删除了if (err) ,那么它将被另一个声明该文档不存在的语句捕获。

Using Mongoose 5.4.0, have tried removing the if statement but just gets caught by the others saying it doesn't exist. 使用Mongoose 5.4.0,曾尝试删除if语句,但被其他人捕获,说它不存在。 I'm also using discord.js, so wherever it says message.reply or message.channel.send - it just means that it will send a message to a channel - this has nothing to do with the error. 我也在使用discord.js,所以无论它说出message.reply还是message.channel.send只是意味着它将向通道发送一条消息-与错误无关。

guildModel.findOne({"GuildName": GuildSearch}).then((err, result) => {
    if (result) {
        let guildEmbed = new Discord.RichEmbed()
        .setTitle(GuildSearch)
        .setColor("00ff65")
        .setDescription(result.GuildDescription);
        return message.channel.send(guildEmbed);
     } else {
        let NoDoc = new Discord.RichEmbed()
        .setTitle("Oops!")
        .setDescription(`<@!${message.author.id}>, There is no Server with the name ${GuildSearch} recorded with me.`)
        .setColor("ff7f00")
        .setFooter("Developed By William#8495");
         return message.channel.send(NoDoc);
      };
}).catch(err => {
    return message.reply("Error: " + err);
});

It should just send a field of the document named GuildDescription, but it sends Error: and the whole document. 它应该只发送一个名为GuildDescription的文档字段,但是会发送Error:和整个文档。

You are using "Promise style" query so the first argument should be result , not err : 您正在使用“承诺样式”查询,因此第一个参数应为result ,而不是err

guildModel.findOne({ "GuildName": GuildSearch }).then(result => {
}).catch(err => {
  console.log(err)
})

Or you could use the other syntax: 或者您可以使用其他语法:

guildModel.findOne({ "GuildName": GuildSearch }, (err, result) => {
  if (err) console.log(err)
});

Mongoose documentation 猫鼬文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM