question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot reuse document after delete/clearFirestoreData

See original GitHub issue

Environment info

@firebase/testing: 0.20.9 firebase-tools: 8.6.0 mocha: 8.0.1 typescript: 3.9.6 Platform: Windows 10

Test case

I have a test suite that runs against the firestore emulator. I have a need to reuse the same document when testing.

When I run the following test (simplified here) …

import * as firebase from '@firebase/testing';

describe('My Test Suite', function () {
    const PROJECT_ID='my-project-id';
 
   it('Should work all the time...', async () => {
        const db = firebase.initializeAdminApp({ projectId: PROJECT_ID }).firestore();
        const ref = db.doc('testcollection/mytest');
        await doSomethingWithinTransaction(db, ref);
        await doSomethingWithinTransaction(db, ref);
        await ref.delete(); // Or await firebase.clearFirestoreData({ projectId: PROJECT_ID });
        console.log('will break on next line...')
        await doSomethingWithinTransaction(db, ref);
    });

    async function doSomethingWithinTransaction(db: firebase.firestore.Firestore, ref: firebase.firestore.DocumentReference) {
        await db.runTransaction(async transaction => {
            const doc = await transaction.get(ref);
            if (!doc.exists) {
                transaction.set(ref, { prop1: '123' });
            }
        });
    }
}

Steps to reproduce

  1. Run the emulators (firestore)
  2. Run the testcase e.g. mocha mytestfile.js

Expected behavior

The test should run - i.e. Should be able to delete then reuse the same document.

Actual behavior

The test fails with this error in the Terminal window (vscode)

FirebaseError: 9 FAILED_PRECONDITION: the stored version (1596517656046369) does not match the required base version (0)

Here is the content of firestore-debug.log

API endpoint: http://localhost:8080
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:

   export FIRESTORE_EMULATOR_HOST=localhost:8080

Dev App Server is now running.

Aug 04, 2020 3:07:23 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:07:23 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:07:23 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:07:23 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
Aug 04, 2020 3:07:24 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:07:24 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:07:33 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
Aug 04, 2020 3:07:37 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError
INFO: operation failed: the stored version (1596517656046369) does not match the required base version (0)
Aug 04, 2020 3:07:37 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError
INFO: operation failed: the stored version (1596517656046369) does not match the required base version (0)
Aug 04, 2020 3:07:38 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError
INFO: operation failed: the stored version (1596517656046369) does not match the required base version (0)
Aug 04, 2020 3:07:41 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError
INFO: operation failed: the stored version (1596517656046369) does not match the required base version (0)
Aug 04, 2020 3:07:45 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError
INFO: operation failed: the stored version (1596517656046369) does not match the required base version (0)
Aug 04, 2020 3:07:48 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError
INFO: operation failed: the stored version (1596517656046369) does not match the required base version (0)
Aug 04, 2020 3:07:55 PM io.netty.channel.DefaultChannelPipeline onUnhandledInboundException
WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.net.SocketException: Connection reset
	at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:345)
	at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:376)
	at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
	at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:832)

Aug 04, 2020 3:08:36 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:09:36 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:10:36 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:11:36 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:12:37 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:13:37 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Aug 04, 2020 3:14:37 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gbhallcommented, Mar 24, 2021

I just spent an inordinate amount of time thinking I wasn’t understanding transactions properly.

Had this same issue using the Swift SDK and emulator. No longer using the emulator and it runs as expected.

Same issue when clearing data within Firestore in the emulator and getting the error does not match the required base version

0reactions
scottcrossencommented, Aug 12, 2020

Yeah looks like a bug! I think athoma13 found the underlying issue too ( thanks alot for looking!).

Unless anyone has objections, I’m going to close this bug as a duplicate of #1971. Please reopen if I’ve missed something!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating same document id after delete it - Stack Overflow
Have a look at the following answer: Firestore's auto-ID fields are designed to statistically guarantee that no two clients will ever ...
Read more >
Delete data from Cloud Firestore - Firebase
When you delete a document, Cloud Firestore does not automatically delete the documents within its subcollections. You can still access the subcollection ...
Read more >
Firebase Firestore Tutorial #5 - Deleting Data - YouTube
Hey all, in this Firebase Firestore tutorial I'll show you how to delete data from the database. ‍ Course Links:- VS...
Read more >
Getting Started with Firebase 9 #5 - Adding & Deleting ...
Hey all, in this Firebase 9 tutorial I'll show you how to add new documents (save them) and also how to delete documents...
Read more >
Adding data | Firestore - Google Cloud
Create an empty document with an automatically generated identifier, and assign data to it later. This guide explains how to use the set,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found