我正在尝试在Firebase RealTime数据库上发出2个请求。首先,我得到一个ID,然后在第二个请求中得到一个包含该ID的帖子。
我尝试用wait替换第一个Promise,但是没有用。我设法使代码在另一个Promise中与一个Promise一起工作,但是我不明白为什么它不能像这样工作,而且我经常偶然发现这个烦人的错误,所以现在该了解为什么它发生以及如何发生了修复它(避免避免!)
exports.getFirstPosts = functions.https.onRequest((req, res) => {
let dbRef = admin.database().ref();
return dbRef.child('/regions/').child(req.query.region)
.limitToFirst(1)
.once('value')
.then((snapshot) => {
let data = snapshot.val();
let message = "empty"
if(data === null || data === undefined) {
message = "No-posts-in-region"
}else{
message = "success"
}
console.log(data);
let postsIdsArray = [];
for(let id in data){
postsIdsArray.push(id);
console.log('x',id);
}
let postData = await dbRef.child('/posts').child(postsIdsArray[0]).once('value')
return res.status(200).json({
version: "check if data is null 2post update",
message: message,
data: data,
arr: postsIdsArray,
post: snapshot.val(),
});
});
});
这是我得到的错误:
C:\Users\marius\Desktop\CloudF\functions\index.js
77:34 error Parsing error: Unexpected token dbRef
✖ 1 problem (1 error, 0 warnings)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\marius\AppData\Roaming\npm-cache\_logs\2019-04-26T16_25_42_235Z-debug.log
events.js:183
throw er; // Unhandled 'error' event
^
Error: spawn npm --prefix "%RESOURCE_DIR%" run lint ENOENT
at notFoundError (C:\Users\marius\AppData\Roaming\nvm\v8.12.0\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\marius\AppData\Roaming\nvm\v8.12.0\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:40:16)
at ChildProcess.cp.emit (C:\Users\marius\AppData\Roaming\nvm\v8.12.0\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:27:25)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
Error: functions predeploy error: Command terminated with non-zero exit code1
Having trouble? Try firebase deploy --help