我是新来的等待者。我只是不知道如何处理所有等待错误和拒绝。总是有来自shopify-api.js函数的无法处理的拒绝,我无法打印出所有由我创建的index.js中间件引起的错误吗?
如何打印所有错误?我在等待正确处理吗?
index.js
const functions = require('firebase-functions');
const app = require('express')();
const cors = require('cors');
// Automatically allow cross-origin requests
app.use(cors({ origin: true }));
const asyncMiddleware = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch((err) => {
console.error(err);
res.json(err)
})
.catch(next)
};
app.get('/v1/Test/Shopify', asyncMiddleware ( async (req, res, next) => {
req.params.workshopId = "TEST_CR"
req.params.date = "2018-11-30"
req.params.startTime = "11:00"
let result1 = await ShopifyWorkshop.AddDate(req, res, next)
console.log("ShopifyWorkshop.AddDate: Success" .green)
res.status(200).json({message: "Success"})
}));
exports.AddDate = async (req, res, next) => {
await Helper.CheckParamsIsNull(req.params)
let wsId = req.params.workshopId
let wsDate = req.params.date
let wsTime = req.params.startTime
// Check if workshop is a level
if (Helper.IsWorkshopType(wsId)) {
return await Promise.all(WORKSHOP_TYPE[wsId].levelNames.map( async (typeName) => {
WORKSHOP_CATEGORY[typeName].codeNames.map( async (codeName) => {
for (let wsId in WORKSHOP_INFO) {
if (WORKSHOP_INFO[wsId].codeName === codeName) {
addVariant(wsId, wsDate, wsTime)
}
}
})
}))
} else if (Helper.IsWorkshopCategory(wsId)) {
return await Promise.all(WORKSHOP_CATEGORY[wsId].codeNames.map ( async (codeName) => {
for (let wsId in WORKSHOP_INFO) {
if (WORKSHOP_INFO[wsId].codeName === codeName) {
await addVariant(wsId, wsDate, wsTime)
}
}
}))
} else {
return await addVariant(wsId, wsDate, wsTime)
}
}
shopify-api.js
exports.AddAProductVariant = async (id, options) => {
console.log("Adding variant to product..." + options.option1)
let result = await shopify.productVariant.create(id, options)
console.log("Added product variant: " + options.option1)
return result
}
答案 0 :(得分:0)
在线
async (typeName) => {
WORKSHOP_CATEGORY[typeName].codeNames.map( async (codeName) => {
…
})
}
您正在创建一个诺言数组,然后忽略它们。在它们周围添加另一个Promise.all
包装器,以便将它们对所有对象的承诺返回到外部map
:
(typeName) => {
return Promise.all(WORKSHOP_CATEGORY[typeName].codeNames.map( async (codeName) => {
// ^^^^^^^^^^^^^^^^^^
…
}))
}