0

first actual question here.

I am working on a loopback project. I migrated from MongoDB to Firebase sucessfully. Swapped loopback mongoDB connector by the loopback firestore connector,run some POST methods, worked fine, data added to firebase.

The problem is that Firebase does not include the autogenerated document ID inside the document itself like MongoDB did.

My question is how can I add each document's ID inside the same document itself, so my old loopback queries (e.g:localhost:3000/api/test/0001/tasks) work the way they were.

Edit: I have been trying to work on the connector directly on github, that way none would face this problem again whilst fixing it for me. I found this:

create(model, data, callback) {
    this.db.collection(model).add(data).then(ref => callback(null, ref.id)).catch(err => callback(err));
}

I cannot figure out a way to make it work

Haythem
  • 384
  • 1
  • 12
  • POST returns the new document ID. If you need the ID in the document, there are some things you can do to add it during create/write (triggers, patch) or aggregate during query (client side) – James Poag Nov 03 '18 at 12:15
  • @JamesPoag how can I get what POST returns. this is the function I had: addObject(object){ return this.http.post(this.apiBaseURL + 'objectlist', object) } – Haythem Nov 03 '18 at 12:46
  • POST returns `application/json` with `{ result : { ... } }` Use Postman or similar to see response body. – James Poag Nov 03 '18 at 15:45
  • This is the actual method from the connectore: create(model, data, callback) { this.db.collection(model).add(data).then(ref => callback(null, ref.id)).catch(err => callback(err));} Is there anyway to create a new object containing {id:docId, data:data} and then use set(data) instead of add(data)? – Haythem Nov 04 '18 at 07:39
  • Perhaps I am misunderstanding you. See this: https://stackoverflow.com/questions/46844907/firestore-is-it-possible-to-get-the-id-before-it-was-added – James Poag Nov 04 '18 at 12:53

1 Answers1

0

I decided to rollback to my old architecture (mongodb) and use the loopback authentication. The reason I decided to do this is after I managed to get the ID from the POST response body, I added an update request to actually insert the ID as a field inside the document, Firebase return an error, saying I cant change the document actual name. Therefor this question is no longer helpful to me. I will keep the question open and continue the discussion in case someone else have the problem or another want to elaborate. Thank you

Haythem
  • 384
  • 1
  • 12