0

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);
}));
Gigi101
  • 139
  • 1
  • 11
  • You can persist this shipment data in a DB of your choice. It could go for a `NoSQL` option like `Mongodb`, `DynamoDB` etc. or you could go for a `RDBMS` like `MySQL`, `PostgreSQL`. Then you could query the data from the DB based on the `shippingId` returned from the `POST` url. – Salvino D'sa Jun 08 '22 at 06:50
  • HI, so I tried: ```then(function(shipment) { const shippingId = shipment.object_id; const userId = to.userId; const user = User.findById(userId); if (user) { user.shippingId = shippingId; } const updatedUser = user.save(); })``` and added ```shippingId``` to my user database in mongodb, but I got this error: ```TypeError: user.save is not a function``` – Gigi101 Jun 08 '22 at 06:55
  • okay, your question should have been this rather the what you've posted above. Nevertheless, it means that you've not initialized the user model correctly. Just by looking at this code, I can see that you're not using `await` to wait for results from db. Refer to [this](https://zellwk.com/blog/crud-express-mongodb/) tutorial for better understanding. – Salvino D'sa Jun 08 '22 at 06:59
  • Okay, so I originally had: ```const user = await User.findById(userId); if (user) { user.shippingId = shippingId; } const updatedUser = await user.save();```, but I was getting ```SyntaxError: Unexpected reserved word``` for await – Gigi101 Jun 08 '22 at 07:02
  • 1
    Yes, that's because you must have used `await` inside a non `async` function. I can't tell exactly without seeing your whole code. – Salvino D'sa Jun 08 '22 at 07:06
  • Okay, thank you! That was the issue. I didn't realize that I needed another ```async``` because I already had one after my ```expressAsyncHandler```, but that makes sense that I would need to include one before a new function – Gigi101 Jun 08 '22 at 07:28

0 Answers0