我希望更改几个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');
});
};
答案 0 :(得分:2)
在WooCommerce中,有一种名为sbrk(2)
的异步调用方法,您可以尝试以下方法:
postAsync
每种方法都可以使用,只需将Async添加到方法名称即可。
您可以在此处查看文档:{{3}}
如果它不起作用,您可以随时使用它来堆叠如下调用:
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);
});