我试图从外部api获取json数据并将其保存在firebase firestore数据库中。当我运行该函数时,我得到以下错误
my error log
' $ firebase deploy
===部署到'borsa-app-f6921'…
i部署函数运行命令:npm——prefix "$RESOURCE_DIR"运行线头
> lint
> eslint .
+ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+ artifactregistry: required API artifactregistry.googleapis.com is enabled
+ functions: required API cloudbuild.googleapis.com is enabled
+ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing codebase default for deployment
! functions: package.json indicates an outdated version of firebase-functions.
Please upgrade-using npm install --save firebase-functions@latest in your functions directory.
! functions: Please note that there will be breaking changes when you upgrade.
i functions: preparing functions directory for uploading...
i functions: packaged C:UsersyusufDesktopbulut-fonksiyonlarifunctions
(75.69 KB) for uploading
+ functions: functions folder uploaded successfully
The following functions are found in your project but do not exist in your local source code:
myFunction(us-central1)
If you are renaming a function or changing its region,
it is recommended that you create the new function first before deleting the old one
to prevent event loss. For more info, visit https://firebase.google.
com/docs/functions/manage- functions#modify
? Would you like to proceed with deletion? Selecting no will continue the rest of
the deployments. Yes
i functions: creating Node.js 16 function saveDataToFirestore(us-central1)...
i functions: deleting Node.js 16 function myFunction(us-central1)...
+ functions[myFunction(us-central1)] Successful delete operation.
Build failed: ...lid: lock file's google-p12-pem@3.1.4 does not satisfy google-p12-pem@2.0.5
npm ERR! Missing: node-forge@0.10.0 from lock fi le
npm ERR! Missing: has-tostringtag@1.0.0 from lock file
npm ERR! Invalid: lock file's yallist@4.0.0 does not satisfy yallist@3.1.1
npm ERR! Missing: define-properties@1.1.4 from lock file
npm ERR! Missing: has-property-descriptors@1.0.0 from lock file
npm ERR! Missing: mimic-fn@2.1.0 from lock file
npm ERR! Missing: duplexify@4.1.2 from lock file
npm ERR! Missing: functions-have-names@1.2.3 from lock file
npm ERR! Invalid: lock file's http-proxy-agent@5.0.0 does not satisfy http-proxy-agent@4.0.1
npm ERR! Invalid: lock file's uuid@8.3.2 does not satisfy uuid@7.0.3
npm ERR! Invalid: lock file's @tootallnate/once@2.0.0 does not satisfy @tootallnate/once@1.1.2
npm ERR! Missing: is-bigint@1.0.4 from lock file
npm ERR! Missing: is-boolean-object@1.1.2 from lock file
npm ERR! Missing: is-number-object@1.0.7 from lock file
npm ERR! Missing: is-symbol@1.0.4 from lock file
npm ERR! Missing: has-bigints@1.0.2 from lock file
npm ERR! Missing: is-weakmap@2.0.1 from lock file
npm ERR! Missing: is-weakset@2.0.2 from lock file
npm ERR! Missing: available-typed-arrays@1.0.5 from lock file
npm ERR! Missing: for-each@0.3.3 from lock file
npm ERR! Missing: gopd@1.0.1 from lock file
npm ERR! Missing: is-typed-array@1.1.10 from lock file
npm ERR! Missing: is-callable@1.2.7 from lock file
npm ERR! Missing: p-try@2.2.0 from lock file
npm ERR! Missing: core-util-is@1.0.3 from lock file
npm ERR! Missing: isarray@1.0.0 from lock file
npm ERR! Missing: process-nextick-args@2.0.1 from lock file
npm ERR! Missing: safe-buffer@5.1.2 from lock file
npm ERR! Missing: string_decoder@1.1.1 from lock file
npm ERR! Missing: @firebase/auth-interop-types@0.1.5 from lock file
npm ERR! Missing: @firebase/util@0.3.2 from lock file
npm ERR! Missing: @firebase/component@0.1.19 from lock file
npm ERR! Missing: @firebase/database-types@0.5.2 from lock file
npm ERR! Missing: tslib@1.14.1 from lock file
npm ERR! Missing: @firebase/app-types@0.6.1 from lock file
npm ERR!
npm ERR! Clean install a project
npm ERR!
npm ERR! Usage:
npm ERR! npm ci
npm ERR!
npm ERR! Options:
npm ERR! [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle]
npm ERR! [-E|--save-exact] [-g|--global] [--global-style] [--legacy-bundling]
npm ERR! [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
npm ERR! [--strict-peer-deps] [--no-package-lock] [--foreground-scripts]
npm ERR! [--ignore-scripts] [--no-audit] [--no-bin-links] [--no-fund] [--dry-run]
npm ERR! [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm ERR! [-ws|--workspaces] [--include-workspace-root] [--install-links]
npm ERR!
npm ERR! aliases: clean-install, ic, install-clean, isntall-clean
npm ERR!
npm ERR! Run "npm help ci" for more info
npm ERR! A complete log of this run can be found in:
npm ERR! /www-data-home/.npm/_logs/2022-12-14T11_02_18_814Z-debug-0.log; Error ID: beaf8772
Functions deploy had errors with the following functions:
saveDataToFirestore(us-central1)
i functions: cleaning up build files...
! functions: Unhandled error cleaning up build images. This could result in a small
monthly bill if not corrected. You can attempt to delete these images
by redeploying or you can delete them manually
at https://console.cloud.google.com/artifacts/docker/borsa-app-f6921/us-central1/gcf-artifacts
Error: There was an error deploying functions
这是我的代码
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const axios = require("axios");
admin.initializeApp();
const db = admin.firestore();
exports.saveDataToFirestore = functions.https.onRequest((request, response) => {
axios.get("https://api.coingecko.com/api/v3/coins/markets? vs_currency=usd&order
=market_cap_desc&per_page=100&page=1&sparkline=true")
.then((res) => {
db.collection("data").add(res.data)
.then(() => {
response.send("successful");
})
.catch((err) => {
response.send("Error " + err);
});
})
.catch((err) => {
response.send("Error REST API: " + err);
});
});
你能改变你的节点14而不是16,让我知道如果它工作。如果14不行,那就选12或10。由于您使用的是firebase 8,这可能是一个兼容性问题
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"firebase": "^8.10.0",
"firebase-admin": "^8.13.0",
"firebase-functions": "^3.14.1"
},
"private": true
}
下面是一个可行的包。
{
"name": "functions",
"scripts": {
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "lib/index.js",
"dependencies": {
"@google-cloud/firestore": "^4.8.1",
"@google-cloud/logging": "^9.2.1",
"@google-cloud/storage": "^5.3.0",
"@google-cloud/tasks": "^2.3.0",
"@sendgrid/mail": "^7.4.2",
"cors": "^2.8.5",
"firebase": "^8.2.0",
"firebase-admin": "^9.4.2",
"firebase-functions": "^3.15.7",
},
"devDependencies": {
"@types/firebase": "^3.2.1",
"typescript": "^4.1.3"
},
"private": true
}
你可以尝试在这里安装firebase, firebase-admin和firebase-functions的版本,这是为我的项目工作。我可以展开函数。在部署
之前执行npm install
按如下方式更新函数
exports.saveDataToFirestore = functions.https.onRequest((request, response) => {
axios.get("https://api.coingecko.com/api/v3/coins/markets? vs_currency=usd&order
=market_cap_desc&per_page=100&page=1&sparkline=true")
.then((res) => {
console.log("res.data", res.data); // make sure you check logs for this, you have make sure you res.data is not undefined
// we need a unique identified, I will use timestamps
const id = new Date().getTime(); // this will be unique
db.collection("data").doc(id).set(res.data,{merge: false}) // this will create a new doc
.then(() => {
response.send("successful");
})
.catch((err) => {
response.send("Error " + err);
});
})
.catch((err) => {
response.send("Error REST API: " + err);
});
});