【发布时间】:2021-04-19 23:11:43
【问题描述】:
在错误处理方面是个新手。
我在此代码中收到(类型“未定义”不可分配给类型“字符串”)错误
编辑:如果这有助于您理解问题,我决定添加整个代码页面。
type AuthClient = Compute | JWT | UserRefreshClient;
function isValidType(type: string): boolean {
return (
type === 'IMPORT_DATA' || type === 'EXPORT_MODEL' || type === 'TRAIN_MODEL'
);
}
/**
* A function to check & update progress of a long running progression
* in AutoML.
*/
export const checkOperationProgress = functions.https.onRequest(
async (request, response) => {
const operationType = request.query['type'];
if (!operationType) {
response.status(404).json({ error: 'Operation `type` needed' });
return;
}
if (!isValidType(operationType)) {
^^^ ERROR ABOVE
response.status(400).json({
error: 'type should be one of IMPORT_DATA, EXPORT_MODEL, TRAIN_MODEL',
});
return;
}
try {
const client = await auth.getClient({ scopes: [AUTOML_API_SCOPE] });
const snapshot = await admin
.firestore()
.collection('operations')
.where('type', '==', operationType)
.where('done', '==', false)
.get();
if (snapshot.empty) {
response.status(200).json({
success: `No pending operations found for type ${operationType}`,
});
return;
}
// for each operation, check the status
snapshot.docs.forEach(async doc => {
await updateOperation(doc, client);
});
response.status(200).json({
success: `${snapshot.docs.length} operations updated: ${operationType}`,
});
} catch (err) {
response.status(500).json({ error: err.toJSON() });
}
}
);
知道我能做些什么吗?
【问题讨论】:
-
从代码中,你分享我只能说
dataset显然是一个字符串数组。因此,您可能应该将其减少为单个字符串。发送错误响应后,您也不会在第二个示例中返回。 -
你的 isValidType 和 generateLabel 函数是什么样子的?
-
嘿,抱歉。如果这有助于您理解问题,我已经添加了整个页面的代码。
-
我不确定为什么会发生这种情况,我无法重现它。您应该通过返回
if(!operationType)案例来消除undefined的可能性。 -
@LindaPaiste 我对如何实现这一点知之甚少,您能否详细说明我应该尝试做什么?
标签: node.js string typescript firebase