【发布时间】:2021-12-25 06:07:38
【问题描述】:
所以,过去几天我一直在尝试添加具有自定义文档 ID 的集合大约 2 天,但我没有成功。
我想使用节点创建一个分析集合,但它从未出现在数据库中。这是我的代码:
const onSubmit = async (_values) => {
setIsLoading(true);
setError(null);
const data = { name: '', pdfUrl: '' };
if (name != '') {
data['name'] = name;
if (pdfUrl) {
data['pdfUrl'] = pdfUrl;
// alert(JSON.stringify(data));
let res = await createAnalysis({ name, pdfUrl });
console.log(res);
alert(JSON.stringify(res));
let json = res.then((json) => console.log(json));
console.log(json);
} else {
addToast({
title: 'No Title created',
description: 'You must create a title to proceed.',
type: 'error',
});
}
} else {
addToast({
title: 'No pdf selected',
description: 'You must selected a pdf to proceed.',
type: 'error',
});
}
};
它应该执行一个名为createAnalysis 的函数。这是函数:
const createAnalysis = async (data: {
name: string;
pdfUrl: string;
}): Promise<any> => {
const name = data.name;
const pdfUrl = data.pdfUrl;
const id = makeId(name);
const ownerId = auth.currentUser.uid;
}
];
// alert(JSON.stringify({ id, name, pdfUrl, ownerId, tools }));
try {
const analysisRef = await db.collection('analysis');
const analysisDoc = await analysisRef.doc(id);
const analysisCreate = await analysisDoc.set({
id,
name,
pdfUrl,
ownerId,
tools,
});
console.log(analysisCreate);
// alert(JSON.stringify(analysisCreate));
return await analysisCreate;
} catch (error) {
// alert(JSON.stringify(error));
console.error(error);
return error;
}
};
它甚至没有显示错误,所以我不知道如何解决这个问题。如果有人知道怎么做,请告诉我。
【问题讨论】:
-
在每条语句前添加
await无济于事。您可以从await db.collection('analysis')、await analysisRef.doc(id)和return await analysisCreate中删除await关键字。 -
@FrankvanPuffelen,不幸的是,它仍然不起作用。
标签: node.js reactjs firebase google-cloud-firestore react-typescript