get shipment.object_id from post api

61 views Asked by At

I'm working on a product using node.js in my backend. In this project, I'm trying to integrate Shippo. Currently, I am able to get the shippo.shipment.create {...} to work. From there I get a shipment.object_id when I console.log(shipment). My issue is that I need to have a get api call to allow customers to choose which shipping rate they prefer. How can I obtain this shipment.object_id outside of my post api call, so that I can retrieve the shipping rates to send to the customer? I would really appreciate any help or advice. Thank you!

I tried doing this by adding shippingId to my user schema and saving it to my Mongodb database:

.then(function(shipment) 
{ const shippingId = shipment.object_id; 
const userId =  to.userId; 
const user = await User.findById(userId); 
 if (user) { user.shippingId = shippingId; } 
const updatedUser = await user.save(); 
}

but I was getting an error SyntaxError: Unexpected reserved word.

import express from 'express';
import shippo from 'shippo';
import expressAsyncHandler from 'express-async-handler';
const shippoToken = env.process.SHIPPO_TOKEN;
const shippoClient = shippo(shippoToken);

const shippoRouter = express.Router();


shippoRouter.post(
    '/', 
expressAsyncHandler(async (req, res) => {
const from = req.body.address
var addressFrom  = {
    "name": from.name,
    "street1": from.street1,
    "city": from.city,
    "state": from.state,
    "zip": from.zip,
    "country": from.country,
};


const to = req.body.name
var addressTo = {
    "name": to.name,
    "street1": to.street1,
    "street2": to.street2,
    "city": to.city,
    "state": to.state,
    "zip": to.zip,
    "country": to.country,
    "phone": to.phone,
    "email":to.email,
    "validate": true,
};

var parcel = {
    "length": "5",
    "width": "5",
    "height": "5",
    "distance_unit": "in",
    "weight": "1.5",
    "mass_unit": "lb"
};


shippoClient.shipment.create({
    "address_from": addressFrom,
    "address_to": addressTo,
    "parcels": [parcel],
    "async": false
})
.catch(function(err) {
    // Deal with an error
    console.log("There was an error creating customs declaration: %s", err);

}).then(function(shipment) {
    const shippingId = shipment.object_id
console.log(shipment)
console.log(shippingId)
})

}));

shippoRouter.get (
    '/shippingInfo', 
expressAsyncHandler(async (req, res) => {
   // shippo.shipment.retrieve(shipment.object_id);
}));
0

There are 0 answers