ฉันประสบปัญหาแปลก ๆ ที่นี่ ฉันสามารถบันทึกข้อมูลผ่านพังพอนได้ แต่ไม่สามารถสืบค้นได้ นี่คือรหัส:
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) UnhandledPromiseRejectionWarning: การปฏิเสธสัญญาที่ไม่สามารถจัดการได้ (id การปฏิเสธ: 1): ValidationError: CastError: Cast to String ล้มเหลวสำหรับค่า "{ _id: 58dd019b1a06731b0990b878, ร้านค้า: 'ชื่อร้านค้า-ที่นี่', หมวดหมู่: [], __v: 0 }" ที่เส้นทาง "ร้านค้า"
ฉันมีสคีมาที่คล้ายกันมากสำหรับคอลเลกชันอื่นๆ ซึ่งทำงานได้ดีแต่ไม่ใช่อันนี้
นี่คือวิธีที่ฉันทำแบบสอบถาม:
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"}).then(store => {/*something here*/}, err => {/*err here*/});
และตรวจสอบว่าเกิดอะไรขึ้น - person Sravan   schedule 30.03.2017'Store-Name-Here'
หรือแม้แต่การจัดการการเรียกกลับที่สำเร็จของ api - person Sravan   schedule 30.03.2017-
(เส้นประ) - person Sravan   schedule 30.03.2017Category.findOne({store: 'Store-Name-Here'}).exec().then(result => { console.log(result);}, err => next(err));
เพียงconsole.log(result)
และตรวจสอบว่ายังมีข้อผิดพลาดอยู่หรือไม่ - person Sravan   schedule 30.03.2017success
หรือerror
? - person Sravan   schedule 30.03.2017'Store-Name-Here'
ตัวอักษร ค่าที่คุณกำลังสืบค้นหรือคุณกำลังส่งตัวแปรบางประเภทที่มีสตริงนั้นหรือไม่ ดูเหมือนว่าคุณกำลังส่งเอกสาร Mongoose แทนที่จะเป็นสตริง นอกจากนี้ คุณมี hooks/มิดเดิลแวร์สำหรับรุ่นCategory
หรือไม่ - person robertklep   schedule 30.03.2017store
ของเอกสารนั้นเป็นวัตถุแทนที่จะเป็นสตริงใช่หรือไม่ - person robertklep   schedule 30.03.2017exec()
ลองใช้ฟังก์ชันโทรกลับสองฟังก์ชันสำหรับsuccess
และerror
ชอบfunction(){}
- person Sravan   schedule 31.03.2017db.categories.find({ store: 'Store-Name-Here' })
(หรือแม้กระทั่งหากมีเอกสารเพียงไม่กี่ฉบับในฐานข้อมูลdb.categories.find()
) - person robertklep   schedule 31.03.2017