且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Node.js中执行Cosmos DB存储过程?

更新时间:2022-10-18 14:50:14

根据API

如屏幕截图所示,/category 是我的分区键路径, fruit 是我的分区键值.您只需要传递水果"字样即可像这样执行 execute 方法:

  container.scripts.storedProcedure(sprocName).execute("fruit",sprocParams) 

I am trying to execute the stored procedure in nodejs. I am using cosmosclient and when I tried executing the code but I'm not able get the response or data back. Here is my piece of code.

private async executeSprocInternal(sprocName, sprocParams) {
        try {
            var sprocLink = this.sprocsUrl + '/' + sprocName;  //sprocLink:: dbs/testDB/colls/test-container/sprocs/test
            var _stdate;
            var _partition:any ={} ;
            if (conf.partition == true)   _partition.partitionKey = 'id';
           
            if (this.showLog == true) _stdate = new Date();
            return new Promise((resolve, reject) => { 
                this.container.scripts.storedProcedure(sprocName).execute(sprocLink,sprocParams,_partition).then((results) =>{
                    if (this.showLog == true) {
                        console.log('Completed Proc ', sprocName, ' in ', ((new Date()).getTime() - _stdate.getTime()), ' msec');
                        }
                    resolve(results);
                })
                .catch((error) =>{
                    console.log("exequeryerror")
                    reject(error)
                })
            });

        } catch (e) {
            console.log(2,e)
            return new Promise((resolve, reject) => {
                 reject(e);
             });
        }
    }

Thanks for the help.

According to API documentation,sprocLink shouldn't be passed.

function execute(partitionKey: any, params?: any[], options?: RequestOptions)

Also, you need to pass value of your Partition Key.(I guess 'id' is your partition key.)

I have tried this and it can work fine, you can have a try:

    async function executeSprocInternal(sprocName, sprocParams) {
        try {
    
            var sprocLink = 'dbs/Test/colls/data/sprocs' + '/' + sprocName;//sprocLink:: dbs/testDB/colls/test-container/sprocs/test
            var _stdate = new Date();;
            var partitionKey  = 'fruit';
        
            return new Promise((resolve, reject) => {
                container.scripts.storedProcedure(sprocName).execute(partitionKey,sprocParams).then((results) => {
                    console.log('Completed Proc ', sprocName, ' in ', ((new Date()).getTime() - _stdate.getTime()), ' msec');
                    resolve(results);
                })
                    .catch((error) => {
                        console.log("exequeryerror")
                        reject(error)
                    })
            });
        } catch (e) {
            console.log(2, e)
            return new Promise((resolve, reject) => {
                reject(e);
            });
        }
    }

    executeSprocInternal("getData", "success").then((message) => {console.log(message);})

Hope this can help you.


Update

Here is my sample:

As the screenshot shows,/category is my Partition key path, fruit is my Partition key value. You just need to pass "fruit" to execute method, like this:

container.scripts.storedProcedure(sprocName).execute("fruit",sprocParams)