我正在制作一个快递系统,其中一个专营权需要将包裹发送给另一个专营权。用户输入包裹需要使用的专营权的名称,我正在获取ID,并将其存储在“ dispatchedTo”中。当我从Angular localStroage获取“ dispatchedBy”的ID时,当用户(Franchise)登录系统时我将其保存在其中。但是,将findById,findOne甚至find应用于从localStorage获取的属性时,将无法正常工作。
//This Part Does not return franchise... franchise object is empty
let franchise = await Franchise.findById(req.body.dispatchedBy);
if(!franchise){
return res.json({error: {message: "Origin Not Found!"},});
}
//This part work... franchise1 is the right object
let franchise1 = await Franchise.findById(req.body.dispatchedTo);
if(!franchise1){
return res.json({error: {message: "Destination Not Found!"},});
}
MongoDB保存ID为'dispatchedBy'和'dispatchedTo'的记录: {“ _id”:“ 5c1fdcd0dc5ce60e8c856ca1”,“ dispatchedBy”:“ 5c1c06c2f458b617506f1ac1”,“ dispatchedTo”:“ 5c1de40419975e0a6c224d6a”,“ dispatchedOn”:“ 2018-12-23TZ:”,“ 2018.12-23TZ:”:“ -23T19:03:50.997Z“,”数据包“:” 5c1ea84ca706ba112c9dbe9e“,” dispatchStatus“:” DisJanu“,”用户“:” Sohail“,” __ v“:0}
但是,当我应用填充时,“ dispatchedBy”将返回null。
router.get('/', async(req, res)=>{
const dispatch = await Dispatch.find().sort('dispatchedOn').populate('packet', 'packetNumber _id').populate('dispatchedBy', 'title _id').populate('dispatchedTo', 'title _id');
return res.json(dispatch);
});
任何可能的解决方法吗?
答案 0 :(得分:0)
因此findById
仅基于_id
字段进行过滤。您可以像尝试findOne
Franchise.findOne({ dispatchedBy: req.body.dispatchedBy });
Franchise.findOne({ dispatchedTo: req.body.dispatchedTo });