我目前正在尝试实现Easypost,但是来自文档的示例是控制台,它在我的前端(localhost:3000)上记录为null .有人知道我做错了什么吗?谢谢..。第一次使用防火墙以及easypost & react
函数/index.js:
const functions = require("firebase-functions");
const EasyPost = require("@easypost/api");
const api = new EasyPost(TEST_API_KEYS);
exports.buyShipping = functions.https.onCall((data, res) => {
const shipment = new api.Shipment({
from_address: {
street1: "417 MONTGOMERY ST",
street2: "FLOOR 5",
city: "SAN FRANCISCO",
state: "CA",
zip: "94104",
country: "US",
company: "EasyPost",
phone: "415-123-4567",
},
to_address: {
name: "Dr. Steve Brule",
street1: "179 N Harbor Dr",
city: "Redondo Beach",
state: "CA",
zip: "90277",
country: "US",
phone: "4155559999",
},
parcel: {
length: 8,
width: 5,
height: 5,
weight: 5,
},
});
shipment.save().then((s) => {
s.buy(shipment.lowestRate()).then((result) => {
return result;
});
});
});
});
前部:
import { initializeApp } from "firebase/app";
import { getFunctions, httpsCallable } from 'firebase/functions';
///
const app = initializeApp(firebaseConfig);
const functions = getFunctions(app);
const buyShipping = httpsCallable(functions, 'buyShipping')
const buy = async () => {
await buyShipping().then((result) => {
/** @type {any} */
console.log(result)
})}
///
<button onClick={buy}>Purchase</button>
发布于 2022-04-12 01:03:03
我发现了问题;我不能在我的防火墙后端的shipment.save函数中返回.这样做是有效的:
exports.buyShipping = functions.https.onCall((data, res) => {
const shipment = new api.Shipment({
from_address: {
street1: "417 MONTGOMERY ST",
street2: "FLOOR 5",
city: "SAN FRANCISCO",
state: "CA",
zip: "94104",
country: "US",
company: "EasyPost",
phone: "415-123-4567",
},
to_address: {
name: "Dr. Steve Brule",
street1: "179 N Harbor Dr",
city: "Redondo Beach",
state: "CA",
zip: "90277",
country: "US",
phone: "4155559999",
},
parcel: {
length: 8,
width: 5,
height: 5,
weight: 5,
},
});
const arrayData = shipment.save().then((s) => {
return s;
});
return arrayData;
});
发布于 2022-04-11 15:49:01
看起来,您是在后端进行控制台日志记录,这不会有任何帮助。相反,您需要从后端返回buyShipping
调用的结果,这样您的前端就可以将其显示在页面或控制台上,记录它或您打算用它做的任何事情。此外,您的两个函数调用不共享相同的名称,因此您实际上不太可能调用后端(根据提供的示例)。buyShipping
是后端函数的名称,但在前端调用sendPackage
。
您将希望您的所有逻辑都驻留在后端节点服务器上,并且除了返回之外什么也不做。然后,所有的“表示”逻辑(显示、日志记录等)都将发生在前端,方法是从后端调用这些函数。
https://stackoverflow.com/questions/71812612
复制相似问题