firestore.Timestamp missing
compojoom opened this issue · 7 comments
If you try to initialize an adminApp with:
const projectId = 'dummy'
const firebase = require('@firebase/testing')
const admin = firebase.initializeAdminApp({projectId, databaseName: projectId})
admin.firestore.Timestamp is missing from admin.firestore.
You can get timestamps via firebase.firestore.Timestamp
Right now, admin apps are implemented as instances of the client SDK with admin permissions. We are working on using the actual admin SDK under the hood. That will address this inconsistency.
Hery Ryan,
Thanks for the reply. I was actually just posting to the js-sdk repository, but will instead continue here.
I understand that I can use the timestamp directly from firebase or firebase admin. However I think that the testing sdk has another bug related to the missing timestamp.
This code here:
export const workWithTime = functions.firestore.document('/crops/{cropId}').onUpdate((change, context) => {
const db = admin.firestore()
const item = change.after.data();
return db
.collection('comments')
.where('item_id', '==', "1")
.get()
.then(querySnap => {
const comments = querySnap.docs.map(doc => {
const comment = doc.data()
comment.cropMeta = {
created: item.created,
}
return comment
})
const writeBatch = db.batch()
comments.forEach(comment => {
console.log(comment)
writeBatch.set(db.collection('comments').doc("1"), comment)
})
return writeBatch.commit().then((result) => {
console.log(result)
})
})
}
Fails with:
FirebaseError: Function WriteBatch.set() called with invalid data. Unsupported field value: a custom object (found in field cropMeta.created)
at new FirestoreError (node_modules/@firebase/firestore/dist/index.node.cjs.js:355:28)
at ParseContext.createError (node_modules/@firebase/firestore/dist/index.node.cjs.js:19726:16)
at validatePlainObject (node_modules/@firebase/firestore/dist/index.node.cjs.js:20111:27)
at UserDataConverter.parseData (node_modules/@firebase/firestore/dist/index.node.cjs.js:19912:13)
at /Users/dd/Documents/Development/Apps/firebase-testing-bug/functions/node_modules/@firebase/firestore/dist/index.node.cjs.js:19955:41
at forEach (node_modules/@firebase/firestore/dist/index.node.cjs.js:457:13)
at UserDataConverter.parseObject (node_modules/@firebase/firestore/dist/index.node.cjs.js:19954:13)
at UserDataConverter.parseData (node_modules/@firebase/firestore/dist/index.node.cjs.js:19913:25)
at /Users/dd/Documents/Development/Apps/firebase-testing-bug/functions/node_modules/@firebase/firestore/dist/index.node.cjs.js:19955:41
at forEach (node_modules/@firebase/firestore/dist/index.node.cjs.js:457:13)
at UserDataConverter.parseObject (node_modules/@firebase/firestore/dist/index.node.cjs.js:19954:13)
at UserDataConverter.parseData (node_modules/@firebase/firestore/dist/index.node.cjs.js:19913:25)
at UserDataConverter.parseSetData (node_modules/@firebase/firestore/dist/index.node.cjs.js:19778:31)
at WriteBatch.set (node_modules/@firebase/firestore/dist/index.node.cjs.js:20605:46)
at comments.forEach.comment (lib/index.js:24:24)
at Array.forEach (<anonymous>)
at db.collection.where.get.then.querySnap (lib/index.js:22:18)
The probelm stems from the snapshot. When testing the function I'm passing a date:
let dummyItem = {
prop: 1,
created: new Date('2019-06-03T06:12:15.643Z'),
}
created is converted to Timestamp. In the firestore code we have a check for the type of the props and prop instanceof firestore.Timestamp fails.
In order to be able to set the batch I have to convert the timestamp to a Date. Then the code in the firestore file is converting Date to Timestamp... We should be able to use the returned timestamps in the snapsho directly.
Excellent repro, thanks. We'll look into this. Seems like the admin SDK is not detecting that Date is a valid Firestore type.
cc: @abeisgoat
How is admin defined in your code? Are you using @firebase/testing in your functions?
Here is the repo with the reproduction code:
https://github.com/compojoom/firebase-testing-bug
here is how I launch the test
https://github.com/compojoom/firebase-testing-bug/blob/master/functions/test/index.test.js
Apologies for the delay. I got your repro working with some combination of:
- bumping the dependencies:
"@firebase/testing": "0.11.5",
"firebase-admin": "8.2.0",
"firebase-functions": "3.1.0"
- removing
initFbin favor offirebase-admin - replacing
const projectId = 'dummy'with my actual project id
Would you mind trying out these changes and seeing if that addresses your issue?
There were a few unrelated bugs in the repo that I had to modify. If you'd like I can open a PR against your repo with my changes.
This issue has been quiet for a while. I'm hoping that's because no one is running into it anymore. Please re-open if you encounter this problem after upgrading to the versions specified above.