0
votes

I am uploading a product with the image using big commerce API. The product is successfully created by API but the image does not. How Can I give the destination path?

I have given the destination path like below

https://store-9gk124wgzn.mybigcommerce.com/dev/product_images

But this does not work.

const storage = multer.diskStorage({
   destination: 'https://store-9gk124wgzn.mybigcommerce.com/dev/product_images',
   filename: function(req, file, cb) {
       cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
   }
});

enter image description here

Here is the full code I am trying to give the path image which it has put the image folder name buddha.jpg but it does not pass the image. const productCreated = function(createnewproduct) { console.log(createnewproduct); const deferred = q.defer(); const postDataOptions = { url: ${BC_STORE_URL}/api/v2/products, method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Basic ' + new Buffer(BC_USER + ':' + BC_TOKEN).toString('base64') }, json: true, body: createnewproduct }; request(postDataOptions, (error, res, body) => { console.log(body); if (!error && res.statusCode == 201) { console.log(createnewproduct); deferred.resolve(createnewproduct); } }); return deferred.promise; }

app.post('/product-created', (req, res) => {

  const createnewproduct = {
    "name": req.body.name,
    "price": req.body.price,
    "categories": [req.body.categories],
    "type": req.body.type,
    "availability": req.body.availability,
    "description": "This timeless fashion staple will never go out of style!",
    "weight": req.body.weight,
    "is_visible": true,
    "id": 549

  };


  productCreated(createnewproduct).then(result => {
    const postImgDataOptions = {
      url: `${BC_STORE_URL}/api/v2/products/${result.id}/images`,
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + new Buffer(BC_USER + ':' + BC_TOKEN).toString('base64')
      },
      json: true,
      body: {
        //http://psdsandbox.com/022/pillow.jpg
        "image_file": "images/buddha.jpg", // this image is put in public folder
        "is_thumbnail": true,
        "sort_order": 0,
        "description": "Hi this is shutter img"
      }
    };
    request(postImgDataOptions, (error, response, body) => {
      console.log(response.statusCode);
      if (!error && response.statusCode == 201) {
        res.send('Done');
      } else {
        res.send('Bad Request');
      }
    });



  });

});
1

1 Answers

0
votes

have you tried using only product_images/?

and instead of using destination: https://... use a callback function something like this

destination: function (req, file, cb) {
    cb(null, 'product_images/')
}

UPDATE: so to upload an image you would have to use formdata


var uploadData = {
    image_file: {
      value: fs.createReadStream("images/buddha.jpg),
      options: {
        filename: "buddha.jpg",
        contentType: "image/jpg"
      }
    }
  };
  var uploadOptions = {
    method: "POST",
    uri: `${BC_STORE_URL}/api/v2/products/${result.id}/images`,
    formData: uploadData
  };
  return request(uploadOptions).then(res => {
    res.send('Done');
 }).catch(function(err){
    res.send('Bad Request');
 })