【发布时间】:2019-04-26 22:27:55
【问题描述】:
所以,我正在使用 spotify API 进行搜索。在本地,我使用 tsc 和 node 测试了我的代码;一切都按预期进行。
但是,当我部署我的 index.ts 时,日志会显示一些奇怪的行为。
在文件的顶部,我有以下内容:
//Load libraries
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase = require("firebase");
const stripe = require('stripe')(functions.config().stripe.token)
const SpotifyWebApi = require('spotify-web-api-node');
//Initialize app
const APP = {
apiKey: "my-API-Key",
authDomain: "My domain auth",
databaseURL: "my databse url",
storageBucket: "my bucket",
messagingSenderId: "my messenger ID"
}
//The Client Credential Flow for Spotify API
const spotifyApi = new SpotifyWebApi({
clientId: 'My cliend ID',
clientSecret: 'My-secret-Key'
});
// Retrieve an access token for Spotify.
spotifyApi.clientCredentialsGrant().then(function (data) {
console.log('All data Spotify API: ' + data.body )
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
}, function (err) {
console.log('Something went wrong when retrieving an access token', err);
})
firebase.initializeApp(APP);
admin.initializeApp(APP)
问题是日志表明我的 index.js 文件中的每个 Firebase-Cloud 函数运行一次 spotify 访问令牌函数。我收到了十几个令牌,每个都显示在日志中,由我的一个 firebase-cloud 函数调用。
当我上传整个 index.ts 时,如何告诉 Firebase 函数只运行一次此代码?当我上传单个函数时不运行它;这也很有用。
谢谢。
【问题讨论】:
标签: javascript typescript firebase google-cloud-functions