最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

'ERROR:'NoneType '对象没有属性'startswith '

运维笔记admin14浏览0评论

'ERROR:\'NoneType \'对象没有属性\'startswith \'

'ERROR:\'NoneType \'对象没有属性\'startswith \'

我正在使用Lambda Face Recognition and Face Detection API via RapidAPI,目前正在通过其api上传图像。下面我有2个场景。两者都具有相同的内容,但是似乎只有一种有效,而另一种却给我一个错误。

我得到的错误如下:

code: 500,
error: 'ERROR: \'NoneType\' object has no attribute \'startswith\''

两者之间的唯一区别是,一种方法从mongo数据库中提取productId和productLink,而另一种则进行了硬编码。下面是代码:

//pulled from db and stored in variables
let productId = product._id.toString(); //5d9ca969835e1edb64cf03d5
let productLink = product.ProductLink; //http://localhost:4000/uploads/1570544614486-test.jpg

//insert data into api
//doesn't work
myFaceDetAPI.trainAlbum(productLink, productId);

//works     
myFaceDetAPI.trainAlbum("http://localhost:4000/uploads/1570544614486-test.jpg", "5d9ca969835e1edb64cf03d5");

我的职能:

this.trainAlbum = (url, id)=>{
     let requestString = "";
     let req = unirest("POST", requestString);
     let imgURL = url;
     let entryId = id

     unirest.post(requestString)
      .header("X-RapidAPI-Key", API_KEY)
      .attach("files", fs.createReadStream(createPath(imgURL)))//creates file path
      .field("album", ALBUM_NAME)
      .field("albumkey", ALBUM_KEY)
      .field("entryid", entryId)
      .end(result => {
        console.log(result.body);
      });
}

问题:

  1. 为什么硬编码方法有效,而从数据库中提取数据的方法无效?
  2. 我该如何做一项从数据库中提取数据的工作?
回答如下:

我发现,通过前端应用程序上传数据后,会将其保存到数据库,并立即在我的图像路径中搜索尚不存在的图像,因此出现了错误。为了解决这个问题,我创建了一个异步函数,该函数将延迟api调用,以便它可以给我的程序一些时间,将图像保存到图像路径。这是代码:

async function customAsyncFunc(productLink, productId){
   console.log(1)
   await delay(5000)
   console.log(2)
   myFaceDetAPI.trainAlbum(productLink, productId)
}

function delay(ms){
    return new Promise(resolve=>{
        setTimeout(resolve,ms)
    })
}

// Defined store route
productRoutes.route('/add').post(function (req, res) {
  let product = new Product(req.body);
  //save to database
  product.save()
    .then(product => {
      let productId = product._id.toString();
      let productLink = product.ProductLink;
      res.status(200).json({'Product': 'Product has been added successfully'});

      //insert data into api    
      //delay sending data to api so that image can be stored into filepath first
      customAsyncFunc(productLink, productId);
    })
    .catch(err => {
        res.status(400).send("unable to save to database");
    });
});
发布评论

评论列表(0)

  1. 暂无评论