Home > database >  Creating new document with Firestore REST API and Local Emulator Suite, Returning Error 404: Problem
Creating new document with Firestore REST API and Local Emulator Suite, Returning Error 404: Problem

Time:10-10

I’m just getting acquainted with Firebase/Firestore as a beginner coder, and I'm attempting to create an integration test for a set of callable functions a friend had written for their project. I am writing a test to automate testing using the Firebase local emulator suite.

Right now, I'm attempting to write a POSt request using Axios that will create a document in a given collection in my local emulator suite, after having received an Id Token from generating an authorized user.

  • The project id is called okane-crud-dev. I’ve created a collection called test.

  • I have created an authenticated user with a given email and password, and generated the unique Id Token from an initial post request:

         interface createPostRequest {
         url: string;
         data: Object;
         config: Object;
     };
    
     //create an instance of a user 
     const createUserInstance : createPostRequest = {
         url: 'http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=hi',
         data: {
             'email': '[email protected]',
             'password': 'mypassword',
             'returnSecureToken': true
         },
         config: {
             'headers':
                 {'Content-Type': 'application/json'}
         },
     };
     const createUserResponse = await axios.post(createUserInstance.url, createUserInstance.data, createUserInstance.config);
    
     const userIdToken = createUserResponse.data.idToken;
     const userLocalId = createUserResponse.data.localId;
    

Up to this point, I have had no issues.

As for the second POST request to create a document, this is my code. I used this post as a reference: Creating new collection and document with Firestore REST API returning HTTP 400

const createDocumentInstance : createPostRequest = {
        url: "https://firestore.googleapis.com/v1beta1/projects/'localhost:8080/okane-crud-dev'/databases/(default)/documents/test",
        data: {
            "fields": {
                "localId": userLocalId,
                'budget': '2000',
            }
        },
        //directly pasted IdToken as using the variable resulted in problem with ' ' error
        config: {
            'headers': 
                {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${userIdToken}`,
                }
    }};
    console.log(createDocumentInstance);

    const createDocument = await axios.post(createDocumentInstance.url, createDocumentInstance.data, createDocumentInstance.config); 
    const docReference = createDocument.data;
    console.log(docReference);

When I attempted to run this, the following error was returned:

    Request failed with status code 404

  at createError (../../node_modules/axios/lib/core/createError.js:16:15)
  at settle (../../node_modules/axios/lib/core/settle.js:17:12)
  at IncomingMessage.handleStreamEnd (../../node_modules/axios/lib/adapters/http.js:293:11)

I'm a beginner and am just starting to learn how to code, so bear with me if this is an easy answer as I'm still figuring out how to debug.

I know that a 404 error means an issue with locating the resource -> and after making some adjustments to the headers, I figured the issue must be in my URL. I’ve tried looking around for other posts that use local emulator suite and POST requests to figure out if there was something wrong with how I wrote the path.

"https://firestore.googleapis.com/v1beta1/projects/'localhost:8080/okane-crud-dev'/databases/(default)/documents/test"

I've been looking at the Firebase documentation closely for creating a document; https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/createDocument#path-parameters

Borrowing from the other post, I’ve tried different variations of where to include the emulator suite port: localhost:8080 and the project id “okane-crud-dev”. But haven’t seemed to figure out. I made sure that the project id was connected to my local emulator suite. Does anyone have any suggestions?

CodePudding user response:

If you're using the Firestore Emulator with the REST API, you should change the base URL https://firestore.googleapis.com/v1 to your localhost http://localhost:8080/v1 then proceed with the path of your Firestore database.

http://localhost:8080/v1/projects/okrane-crud-dev/databases/(default)/documents/test
  • Related