node.js - 无法读取 null Nodejs 的属性“元数据”
问题描述
这是我的错误:
TypeError: Cannot read property 'metadata' of null
at gfs.files.findOne (M:\FinalProject\Commerce\routes\index.js:187:13)
at result (M:\FinalProject\Commerce\node_modules\mongodb\lib\utils.js:414:17)
这是我的代码:
router.get('/:filename', (req,res) => {
const img = req.params.filename; // Filename
gfs.files.findOne({filename: img}, (req,file) =>{
if(file.metadata.brand=="Mango"){
const brand = "Mango";
displayOne(brand);
}
else if(file.metadata.brand=="Cocotail")
{
const brand = "Cocotail";
displayOne(brand);
}
else if(file.metadata.brand==null)
{
console.log("Null");
}
function displayOne(brand)
{
gfs.files.find({'metadata.brand': brand }).toArray((err,files)=>{
if(!file || file.length ===0)
{
return res.status(404).json({
err: 'No files exist'
});
}
if(file.contentType === 'image/jpeg' || file.contentType === 'image/png')
{
file.isImage = true;
}
else
{
res.status(404).json({
err: 'Not an image'
});
file.isImage = false;
}
res.render('singleproduct',{
file:file,
relatedProduct:files, // Related Products
isSearch:0
});
});
}
});
});
请给我有关此错误的任何想法。我无法找出导致此错误的主要原因是什么。我在谷歌上搜索,但没有合适的解决方案。______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
谢谢
解决方案
你有没有安慰你上面得到的东西
(file.metadata.brand=="芒果")
在文件中?似乎你没有得到任何数据
gfs.files.findOne({文件名:img}
尝试这个:
if(file && file.metadata.brand=="Mango"){
const brand = "Mango";
displayOne(brand);
}
else if(file && file.metadata.brand=="Cocotail")
{
const brand = "Cocotail";
displayOne(brand);
}
else if(file && file.metadata.brand==null)
{
console.log("Null");
}
else{
console.log("didinot find value")
}
推荐阅读
- javascript - 在 Django 中执行 Python 中的 PHP 操作
- android - 如果 GPS 在 Flutter 的后台被禁用,应用程序崩溃
- r - RShiny 只有一个过滤器可用
- javascript - 多个文件名称不同?生成唯一ID?
- dc.js - 在 dc.seriesChart 中创建 3 行
- arrays - 遍历 JSON 数据并将其存储在数组中
- python - 为什么在 Windows 和 Linux 上看到的关键代码不同?
- firebase - Flutter 报错的最佳实践
- c# - 根据id创建Button
- jupyter-lab - 始终将 jupyterlab 标准输出发送到 ipywidgets 输出小部件