javascript - 时间戳不适用于 firebase-functions-test

标签 javascript firebase testing google-cloud-firestore firebase-admin

我正在使用 firebase-admin 8.6.0 和 firebase-functions-test 0.1.6,它们在测试中支持 Firestore 时间戳(截至 0.1.5 https://github.com/firebase/firebase-functions-test/releases ),但在尝试时仍然收到错误消息将它们与 test.firestore.makeDocumentSnapshot 一起使用。

有人可以帮助我理解我的实现中的错误吗?

import * as admin from 'firebase-admin';
admin.initializeApp();
const db = admin.firestore();
const settings = { timestampsInSnapshots: true};
db.settings(settings);

const timestamp = admin.firestore.FieldValue.serverTimestamp();
const testingTimestamp1 = admin.firestore.Timestamp.now();
const testingTimestamp2 = admin.firestore.Timestamp.fromDate(new Date);

import * as TestFunctions from 'firebase-functions-test';

const firebaseConfig = {
    databaseURL: 'https://...HIDDEN...',
    projectId: '...HIDDEN...',
    storageBucket: '...HIDDEN...appspot.com',
}

const test = TestFunctions(firebaseConfig, 'service-account-dev.json');

const data({
  timestamp,
  testingTimestamp1,
  testingTimestamp2,
});

const snap = test.firestore.makeDocumentSnapshot(data, path);

const wrapped = test.wrap(processImport);
await wrapped(snap, {params: testParams});

我无法使用三个时间戳选项中的任何一个。后一个我尝试从https://github.com/firebase/firebase-functions-test/pull/28中的@the0rem学习。但无济于事。我总是收到此错误:

Cannot encode [object Object]to a Firestore Value. Local testing does not yet support Firestore geo points.`

最佳答案

当我看到你的问题时,我很兴奋,因为我也遇到了同样的问题。无论如何,这就是我最终解决它的方法。这是我从 Firestore 数据库中保存的 JSON 数据:

const customer = { 
    username: "A8tAz6wdtucMNKvWSgDkx4bquc2", 
    timestamp: { 
        _seconds: 1578762627, 
        _nanoseconds: 828000000 
    }, 
    role: "user" 
};

请注意,timestamp 只是一个普通对象,具有两个属性:_seconds_nanoseconds。这就是错误“无法将 [object Object] 编码为 Firestore 值”的原因。来自,即数据对象 customer 包含另一个对象 timestamp,Firestore 无法解析该对象。为了解决这个问题,我们要做的是确保 timestamp 不是普通对象,而是 admin.firestore.Timestamp 的实例。以下是您如何做到这一点:

const seconds = customer.timestamp._seconds;
const nanosecs = customer.timestamp._nanoseconds;

// create a new Timestamp object passing in the required constructor arguments
const properTimestamp = new admin.firestore.Timestamp(seconds, nanosecs);
customer.timestamp = properTimestamp; // update the timestamp reference in your data object
// Now you can do this
test.firestore.makeDocumentSnapshot(customer, '/users/A8tAz6wdtucMNKvWSgDkx4bquc2')

现在,如果您执行 console.log(customer); 您将看到时间戳对象是 admin.firestore.Timestamp 的实例,这是控制台输出:

// OUTPUT
{
    username: 'A8tAz6wdtucMNKvWSgDkx4bquc2',
    timestamp: Timestamp { _seconds: 1578762627, _nanoseconds: 828000000 },
    role: 'user'
}

PS:我通过检查 Firebase 如何尝试解析数据中的值/对象得到了答案。这是解析失败并引发异常的地方(在文件中:../node_modules/firebase-functions-test/lib/providers/firestore.js:165:15):

if (val instanceof firebase_admin_1.firestore.Timestamp) {
        return {
            timestampValue: val.toDate().toISOString(),
        };
    }

关于javascript - 时间戳不适用于 firebase-functions-test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58867153/

相关文章:

ios - XCTest 在 TestSummaries 中没有 ActivitySummaries

java - Mockito 模拟不适用于此方法。难道我做错了什么?

javascript - 移动 JavaScript 与桌面 JavaScript 不同吗?

java - 无法在 Firebase 中添加时间戳?

Swift Firebase - 将数据库快照转换为数组

ios - 在 Swift 中为 Firebase 数据结构创建类

javascript - 什么是删除大写字母之间的空格但保留单词之间的空格的正则表达式?

javascript - 有一天设置浏览器cookie

javascript - 如何格式化txt文件并导入jquery以在变量内使用

java - AssertionError 空 Selenium 测试