我正在使用基于RapidAPI的Lambda人脸识别与人脸检测API,我目前正在通过他们的api上传图像。下面我有两个场景。两者都有相同的内容,但只有一个似乎有效,另一个给我一个错误。
我得到的错误如下:
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 = "https://lambda-face-recognition.p.rapidapi.com/album_train";
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);
});
}
问题:
发布于 2019-10-08 20:07:16
我发现在通过前端应用程序上传数据时,会将其保存到数据库中,并立即搜索图像路径中还不存在的图像,因此出现了错误。为了解决这个问题,我创建了一个异步函数,它将延迟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");
});
});
https://stackoverflow.com/questions/58292620
复制相似问题