简体   繁体   中英

Azure Function with NodeJS: retrieve id generated by documentDB after a POST request

I have a quite straightforward issue with a AzureFunction. I want to make a POST request and send back to client to newly created object, with it's id created by documentDB. This is what my code looks like:

function.json

   {
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [ "post" ],
      "route": "blocks"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "block",
      "type": "documentDB",
      "direction": "out",
      "databaseName": "digitalnetworkacquisition",
      "collectionName": "blocks",
      "connection": "CosmosDBConnection"
    }
  ],
  "disabled": false
}

index.js

  module.exports = function (context, req) { context.log('Add block to CosmosDB.'); if (req.body) { let block = req.body; context.bindings.block = block; context.res = { body: context.bindings.block, status: 201 } context.done(); return; } context.res = { body: undefined, status: 404 }; context.done(); }; 

This code insert my object in the database but send back to the client the body of my request, which is normal because of the line:

context.bindings.block = block;

That's not what I want. I want to retrieve the inserted object from database and send it back to client, in order to have the created id. I don't know how to do that. Any suggestions?

Thanks a lot!

You can try with this code. When you create the document you should pass the following parameters.

  • collLink: url of the collection = dbLink + collectionId
  • documentDefinition:
  • err: error callback
  • document: created document

Then you can get id of the created document with document.id

collLink = dbLink + '/colls/' + collectionId;

function insertDocuments(collLink, callback) {

    client.createDocument(collLink, documentDefinition, function (err, document) {
            if (err) {
                console.log(err);

            } else {
                console.log('created ' + document.id);

            }
        });

}   

For more examples of documentDb with Node.js you can visit this repo: https://github.com/Azure/azure-documentdb-node/blob/ef53e5f6707a5dc45920fb6ad54d9c7e008a6c18/samples/DocumentDB.Samples.DocumentManagement/app.js#L153-L177

I know this has been answered a while back but it should be possible to set the id either in the call to your Azure Function (AF) or by adding it in your AF. Little known fact but the id is typically generated client side by the SDK unless one is provided. When using the underlying REST API, it is an error to create a document without one.

Load the DocumentDB SDK and use that GUID generator or extract the code from the open sourced SDK code and paste it into your AF.

getGUID = require('documentdb').Base.generateGuidId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM