【发布时间】:2017-08-24 11:03:18
【问题描述】:
我在这里遇到了一个很奇怪的问题,我可以通过猫鼬保存数据但不能进行查询。 代码如下:
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);
当我尝试进行查询时,我收到此错误:
(node:7412) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝(拒绝 id:1):ValidationError:CastError:Cast to 值“{_id:58dd019b1a06731b0990b878,存储: 'Store-Name-Here',类别:[],__v: 0 }" 在路径“store”
我得到了其他集合的非常相似的架构,它们工作正常,但不是这个。
这就是我进行查询的方式:
Category.findOne({store: 'Store-Name-Here'}).exec().then(result => console.log(result), err => console.log(err));
和
Category.find().exec(function(err, result) {
if (err)
return next(err);
console.log(result);
})
【问题讨论】:
-
你是怎么查询的?请添加功能
-
Category.findOne({store: 'Store-Name-Here'}).then(store => {/*something here*/}, err => {/*err here*/} );
-
一次尝试,
Category.findOne({store: "Store"}).then(store => {/*something here*/}, err => {/*err here*/});,看看会发生什么 -
它返回 null
-
错误消失了?当你给的时候?
标签: node.js express mongoose mongoose-schema