cmi*_*mii 1 javascript node.js async-await google-cloud-functions google-cloud-firestore
部署功能时出现错误:error Parsing error: Unexpected token saveLastReview
。
我需要获取一些文档值,然后调用url请求,然后在文档中设置数据。
async function getValue() {
try {
var doc = await admin.firestore().collection('mycollec').doc('mydoc').get();
var data = doc.data()
return data;
} catch(e) {
console.log(e);
return null;
}
}
async function saveLastReview(authorName) {
var rating = "4";
var title = "my title";
var content = "my content";
let data = {
rating : rating,
title: title,
content: content
};
try {
var doc = await admin.firestore().collection('mycollec').doc('mydoc').collection('reviews').doc(authorName).set(data);
return doc;
} catch(e) {
console.log(e);
return null;
}
}
app.get('/hello-world', async(req, res) => {
var data = await getValue();
if (data === null) {
request("https://itunes.apple.com/gb/rss/customerreviews/id=284882215/sortBy=mostRecent/json", function (error, response, body) {
//code to get authorname from the response
var result = await saveLastReview(authorname);
//check if doc was set correctly
//do something
})
}
return res.status(200).send("sent !");
});
module.exports.app = functions.https.onRequest(app);
Run Code Online (Sandbox Code Playgroud)
我对async / await不太熟悉。我没发现问题。
您的回调似乎request
缺少async
关键字。可能会导致您看到的错误,该错误与您所在的行有关await
,这在非异步函数中没有任何意义。
应该可能是:
//...
request("https://itunes.apple.com/...", async function (error, response, body) {
//...
Run Code Online (Sandbox Code Playgroud)
编辑:如评论中所述,也许不是。但是我也注意到这本身saveLastReview
就是一个async
函数,我不知道async
当他们被await
编辑时函数的行为。如果我首先提到的内容不能解决问题,则可能是另一种调查途径。
归档时间: |
|
查看次数: |
67 次 |
最近记录: |