gpt4 book ai didi

javascript - 在 AWS Lambda 中使用 NodeJS 将 Woocommerce API 函数更改为异步/等待

转载 作者:搜寻专家 更新时间:2023-11-01 00:47:55 24 4
gpt4 key购买 nike

我希望更改几个 Woocommerce API 调用,以便每个函数在下一个函数继续之前完成。但是,我不确定如何在不破坏特定 Node Woocommerce API 代码的情况下执行此操作。

https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#create-an-order-note

如何将这两个 Woocommerce POST 函数更改为 Async/Await 以便它与 AWS Lambda 一起使用?

(我已经包含了一个当前在 AWS Lambda 中使用的现有 await 函数作为引用)

const WooCommerceAPI = require('woocommerce-api');
const path = require('path');
const util = require('util');

exports.handler = async (event, context, callback) => {

// AWAIT EXAMPLE in LAMBDA -- Outputs the /tmp/ contents to the console.
const readdirs = util.promisify(fs.readdir);
await readdirs('/tmp/').then((files) => {
console.log('Check tmp contents')', files);
}).catch((err) => {
console.log(err);
});

// Update Woocommerce order
const WooCommerce = new WooCommerceAPI({
url: process.env.WOOAPI_URL, // Your store URL
consumerKey: process.env.WOOAPI_KEY, // Your consumer key
consumerSecret: process.env.WOOAPI_SECRET, // Your consumer secret
wpAPI: true, // Enable the WP REST API integration
version: 'wc/v3' // WooCommerce WP REST API version
});

//Set order as complete
const status = { status: 'completed' };
WooCommerce.post('orders/' + orderId, status, function (err, states, res) {
if (err) throw err;
else console.log('Update WooCommerce order with status');
});

const data = { note: 'Please check www.example.com for full instructions' };
WooCommerce.post('orders/' + orderId + '/notes', data, function (err, data, res) {
if (err) throw err;
else console.log('Manual WooCommerce Order Note');
});

};

最佳答案

在 WooCommerce 中有一个名为 postAsync 的方法用于异步调用,您可以尝试类似的方法:

const status = {
status: 'completed'
};
const ordersResult = await WooCommerce.postAsync(`orders/${orderId}`, status).then((data) => {
console.log('Update WooCommerce order with status');
}, (err) => {
console.log(err);
});

const noteData = {
note: 'Please check www.example.com for full instructions'
};

const notesResult = await WooCommerce.postAsync(`orders/${orderId}/notes`, noteData).then((data) => {
console.log('Manual WooCommerce Order Note');
}, (err) => {
console.log(err);
});

Every method can be used in a promified way just adding Async to the method name.

您可以在此处查看文档:https://github.com/woocommerce/wc-api-node#promified-methods


如果它不起作用,您可以随时使用它来堆叠调用,如下所示:

const status = {
status: 'completed'
};
WooCommerce.post('orders/' + orderId, status, function(err, states, res) {
if (err) throw err;
console.log('Update WooCommerce order with status');
const data = {
note: 'Please check www.example.com for full instructions'
};
WooCommerce.post('orders/' + orderId + '/notes', data, function(err, data, res) {
if (err) throw err;
console.log('Manual WooCommerce Order Note');
});
});

关于javascript - 在 AWS Lambda 中使用 NodeJS 将 Woocommerce API 函数更改为异步/等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56627760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com