在应用程序方面,我可以查询集合并将结果自动广播为界面。 Positions有一个采用接口IPosition的构造方法。
似乎在云功能方面做同样的事情不允许部署功能。很难调试代码,因为必须对其进行部署,并且仅在代码有效时才起作用(本地服务需要一些权限)。
我能够通过删除大部分代码并逐行重新添加来缩小范围,直到偶然发现这一点。
我猜想这与具有类型enum
的属性的接口有关。将position
转换为IPosition也不起作用。
该接口也从另一个模块(父应用程序模块)导入
import { Position } from '../../src/app/models/position';
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { UserRecord } from 'firebase-functions/lib/providers/auth';
admin.initializeApp();
const promisePool = require('es6-promise-pool');
const PromisePool = promisePool.PromisePool;
// const secureCompare = require('secure-compare');
const MAX_CONCURRENT = 3;
const store = admin.firestore();
exports.updateMetrics = functions.https.onRequest((req, res) => {
// const key = req.query.key;
// // Exit if the keys don't match.
// if (!secureCompare(key, functions.config().cron.key)) {
// console.log(
// 'The key provided in the request does not match the key set in the environment. Check that',
// key,
// 'matches the cron.key attribute in `firebase env:get`'
// );
// res
// .status(403)
// .send(
// 'Security key does not match. Make sure your "key" URL query parameter matches the ' +
// 'cron.key environment variable.'
// );
// return null;
// }
// Fetch all user.
return getUsers()
.then(users => {
// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
const pool = new PromisePool(
() => runMetricsAnalysis(users),
MAX_CONCURRENT
);
return pool.start();
})
.then(() => {
console.log('metrics updated');
res.send('metrics updated');
return null;
});
});
/**
* Returns the list of all users.
*/
function getUsers(users: UserRecord[] = [], nextPageToken?: string) {
let tempUsers: UserRecord[] = users;
return admin
.auth()
.listUsers(1000, nextPageToken)
.then(result => {
// Concat with list of previously found users if there was more than 1000 users.
tempUsers = tempUsers.concat(result.users);
// If there are more users to fetch we fetch them.
if (result.pageToken) {
return getUsers(tempUsers, result.pageToken);
}
return tempUsers;
});
}
function runMetricsAnalysis(users: UserRecord[]) {
if (users.length > 0) {
const user = users.pop();
if (user != null) {
return getPositions(user)
.then(positions => {
const metrics = generateMetrics(positions);
console.log('metrics', metrics);
return null;
// return writeMetrics(user.uid, metrics).catch(function(err) {
// console.error(err);
// return null;
// });
})
.catch(function(err) {
console.error(err);
return null;
});
}
return null;
}
return null;
}
/**
* Returns the list of positions for the previous month.
*/
function getPositions(user: UserRecord) {
return store
.collection(`users/${user.uid}/positions`)
.orderBy('postedDate', 'desc')
.get()
.then(querySnapshot => querySnapshot.docs.map(doc => doc.data()));
}
interface IMetrics {
portfolioValue: number;
profitLoss: number;
fees: number;
}
/**
* Generate metrics from positions
*/
function generateMetrics(positions: Array<any>): IMetrics {
let portfolioValue = 0;
let profitLoss = 0;
let fees = 0;
if (positions.length > 0) {
console.log('positions 5', positions);
positions
.map(position => new Position(position))
.map(position => {
portfolioValue += position.positionValue;
profitLoss += position.profitLossClosedQuantity;
fees += position.fees;
});
}
const IMetric = {
portfolioValue: portfolioValue,
profitLoss: profitLoss,
fees: fees
};
return IMetric;
}
位置
export interface IPosition {
...
}
export class Position implements IPosition {
...
constructor(position: IPosition) {
...
}
}
更新:
以前由于某种原因我看不到错误(可能是因为它只是部署了该功能的缓存版本。
Here is the error:
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'Timestamp' of undefined
at Object.<anonymous> (/Users/AceGreen/Library/Mobile Documents/com~apple~CloudDocs/Dev/Web/TradingTracker/functions/lib/src/app/models/position.js:5:33)
at Module._compile (internal/modules/cjs/loader.js:736:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
at Function.Module._load (internal/modules/cjs/loader.js:560:3)
at Module.require (internal/modules/cjs/loader.js:665:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/Users/AceGreen/Library/Mobile Documents/com~apple~CloudDocs/Dev/Web/TradingTracker/functions/lib/index.js:3:20)
at Module._compile (internal/modules/cjs/loader.js:736:30)
position.js翻译
const app_1 = require("firebase/app");
var Timestamp = app_1.firestore.Timestamp;
答案 0 :(得分:0)
我能够解决此问题。我似乎是如何导入时间戳的。
const app_1 = require("firebase/app");
var Timestamp = app_1.firestore.Timestamp;
正确的方法:
const app_1 = require("firebase");
var Timestamp = app_1.firestore.Timestamp;
重要说明:
似乎firebase deploy-如果无法解析当前函数,则只有函数会使用该函数的缓存版本。我之所以这样说,是因为在函数中引用Timestamp时运行lint
不会导致任何错误,并且看起来部署成功。由于我已经部署了相同的功能,因此它似乎使用了缓存版本。
我只有在更换计算机并不得不重新安装firebase-cli并重新部署后才能够发现问题,然后指出了对时间戳的错误引用。