Node.js 转换错误:尝试查找值时转换为字符串失败

Node.js 转换错误:尝试查找值时转换为字符串失败,node.js,express,mongoose,mongoose-schema,Node.js,Express,Mongoose,Mongoose Schema,我在这里遇到了一个非常奇怪的问题,我可以通过mongoose保存数据,但不能进行查询。 代码如下: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const CategorySchema = new Schema({ store : {type: String, required: true, unique: true}, categories : [{ parent : Stri

我在这里遇到了一个非常奇怪的问题,我可以通过mongoose保存数据,但不能进行查询。 代码如下:

const mongoose = require('mongoose');
const Schema   = mongoose.Schema;

const CategorySchema = new Schema({
  store : {type: String, required: true, unique: true},
  categories : [{
    parent : String,
    name : String,
  }],
});

CategorySchema.index({store: 1, update_at: -1});

module.exports = mongoose.model('Category', CategorySchema);
当我尝试执行查询时,出现以下错误:

(节点:7412)未处理的PromisejectionWarning:未处理的承诺 拒绝(拒绝id:1):ValidationError:CastError:Cast to 值“{u id:58dd019b1a06731b0990b878,存储区: “此处存储名称”,类别:[],_v:0}位于路径“存储”

我得到了与其他集合非常相似的模式,它们工作得很好,但不是这一个

以下是我执行查询的方式:

Category.findOne({store: 'Store-Name-Here'}).exec().then(result => console.log(result), err => console.log(err));


我想我发现了问题所在。我试图向名为
CategoryScheme.static.init()
的分类方案中添加一个静态方法。在我去掉这个方法后,一切都很好。我想可能出于某种原因已经有了一个名为
init()
的方法,我创建了一个可能已经覆盖了它的方法。这就是导致错误的原因。

我想我找到了问题所在。我试图向名为
CategoryScheme.static.init()
的分类方案中添加一个静态方法。在我去掉这个方法后,一切都很好。我想可能出于某种原因已经有了一个名为
init()
的方法,我创建了一个可能已经覆盖了它的方法。这就是导致错误的原因。

您是如何查询的?请添加functionCategory.findOne({store:'storename Here'})。然后(store=>{/*somethine Here*/},err=>{/*err Here*/});一次尝试,
Category.findOne({store:“store”})。然后(store=>{/*somethine here*/},err=>{/*err here*/}),并检查Happenst返回的内容nullerror是否消失?当你这么说的时候?你是怎么问的?请添加functionCategory.findOne({store:'storename Here'})。然后(store=>{/*somethine Here*/},err=>{/*err Here*/});一次尝试,
Category.findOne({store:“store”})。然后(store=>{/*somethine here*/},err=>{/*err here*/}),并检查Happenst返回的内容nullerror是否消失?你给我的时候?
Category.find().exec(function(err, result) {
    if (err)
        return next(err);
    console.log(result);
})