【发布时间】:2020-05-05 15:40:40
【问题描述】:
我正在尝试根据用户的消息在数据库中查找特定行,即:catalystname。
在架构中,我已成功地将给定的字符串索引为文本:
const { Schema } = mongoose;
const scheduleMessageSchema = new Schema({
_id: { type: Schema.Types.Oid, auto: true },
catalystname: String,
catalystdesc: String,
catalystquest: String,
date: String,
});
scheduleMessageSchema.index({catalystname: 'text'});
module.exports = mongoose.model('dbcatalyst', scheduleMessageSchema);
我的搜索码:
const Catal = require("../src/models/dbcatalyst.js")
module.exports.run = async (client, message, args) => {
message.content = args.slice(0).join(" ")
Catal.find({$text: {$search: message.content}})
.exec(function(docs){
let embedlogs3 = new Discord.RichEmbed()
.setAuthor(`1`, message.author.displayAvatarURL)
.setDescription(`${docs}`)
.setColor("#33ffff")
message.channel.send(embedlogs3)
/*/ ${collected.first().content}/*/
});
}
并开始在消息中搜索所需的行。机器人成功地完成了它的任务,但是完整地显示了整个文档而不是 1 行。
_id: 5e243704961eb23c106bfb02,
catalystname: 'Чёрный Коготь',
catalystdesc: '0',
catalystquest: '0',
date: '1579430157018',
__v: 0
}
我能以某种方式准确地输出字符串吗? catalystname
【问题讨论】:
-
欢迎来到 StackOverflow。您是否尝试过使用
docs.catalystname? -
@ChrisSatchell
TypeError: Cannot read property 'catalystname' of null -
什么是
docs?是对象还是数组? -
@ChrisSatchell 对象,可能。
标签: javascript mongodb mongoose discord discord.js