[英]AttributeError: 'NoneType' object has no attribute 'is_active'
[英]'ERROR: \'NoneType\' object has no attribute \'startswith\'
我正在通過 RapidAPI 使用Lambda 人臉識別和人臉檢測 API ,我目前正在通過他們的 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");
我的 function:
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);
});
}
問題:
我發現通過前端應用程序上傳數據時會將其保存到數據庫中,並立即在我的圖像路徑中搜索尚不存在的圖像,因此出現錯誤。 為了解決這個問題,我創建了一個異步 function ,它會延遲 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");
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.