简体   繁体   中英

'ERROR: \'NoneType\' object has no attribute \'startswith\'

I am using Lambda Face Recognition and Face Detection API via RapidAPI and I am currently uploading an image via their api. Below I have 2 scenarios. Both have the same content but only one seems to work and the other one gives me an error.

The error I am getting is as follows:

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

The only difference between the two is that one method pulls productId and productLink from a mongo database while the other one is hardcoded. Below is the code:

//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");

my 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);
      });
}

Questions:

  1. Why is the hard-coded method working and the one which pulls data from the database is not?
  2. How do I make the one work that pulls data from the database?

I found out that upon uploading data via the front end application would save it to the database and instantly search for the image in my image path which wouldn't exist yet, hence the error. To counter that problem, I created an async function which would delay the api call so it can give time to my program saving the image to the image path. Here is the code:

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");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM