如何在Node.js中执行Cosmos DB存储过程? [英] How to execute the cosmos db stored procedure in nodejs?
问题描述
我正在尝试在nodejs中执行存储过程.我正在使用cosmosclient,当我尝试执行代码时,但无法获取响应或数据.这是我的代码.
私有异步executeSprocInternal(sprocName,sprocParams){尝试 {var sprocLink = this.sprocsUrl +'/'+ sprocName;//sprocLink :: dbs/testDB/colls/test-container/sprocs/testvar _stdate;var _partition:any = {};如果(conf.partition == true)_partition.partitionKey ='id';如果(this.showLog == true)_stdate = new Date();返回新的Promise((resolve,reject)=> {this.container.scripts.storedProcedure(sprocName).execute(sprocLink,sprocParams,_partition).then((results)=> {如果(this.showLog == true){console.log('Completed Proc',sprocName,'in',(((new Date()).getTime()-_stdate.getTime()),'msec');}解决(结果);}).catch((错误)=> {console.log("exequeryerror")拒绝(错误)})});}抓住(e){console.log(2,e)返回新的Promise((resolve,reject)=> {拒绝(e);});}}
感谢您的帮助.
根据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)
这篇关于如何在Node.js中执行Cosmos DB存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!