I was unable to find correct answer and therefore posting a question.
Return response on Postman is as below:
[
{
"AttemptNumber":1,
"LoginID":test123,
"CurrentStatus":2
}
]
I'm trying to count elements in this array object.
this is what I was doing:
countItems = JSON.pase(responseBody)
for (var i = 1, l = Object.keys(countItems).length; i <=3){
}
but I keep getting 1. 1 is array but I'm looking for count to be 3.
I'll appreciate your expertise.
Thanks.
This should log the number of items from each object in the response array:
let res = pm.response.json()
_.each(res, (obj) => {
console.log(_.keys(obj).length)
})
It's using some methods from Lodash, which is one of the built-in libraries.
https://lodash.com/docs/4.17.15
I am facing a problem with Facebook meseger chat- bot ,problem is with Context variable storing and update .
My code is divided in two parts
_________**********_________
part 1:
var index = 0;
Facebookcontexts.forEach(function(value) {
console.log(value.From);
if (value.From == sender_psid) {
FacebookContext.context = value.FacebookContext;
console.log("Inside foreach "+JSON.stringify(FacebookContext.context));
contextIndex = index;
}
index = index + 1;
});
Here I have created an array named Facebookcontexts to store contexts for different users.This is where I am getting position of the user in a Facebookcontexts array which is used for later .
_________**************_____________
part 2:
if((FacebookContext.context==null)||(Facebookcontexts.find(v=>v.From==sender_psid)==undefined)){
Facebookcontexts.push({"From":sender_psid,"FacebookContext":response.context})
console.log("I am where sender is unknmown"+JSON.stringify(Facebookcontexts)+"\n"+Facebookcontexts.length);
}
else if(Facebookcontexts.find(v=>v.From==sender_psid)!=undefined){
Facebookcontexts[contextIndex].FacebookContext=response.context;
console.log("I am at where I know the sender"+JSON.stringify(Facebookcontexts)+"\n"+Facebookcontexts.length);
}
I am deciding to create a new record or update old one in if and else
Issue:
My Issue is every time if((FacebookContext.context==null)||(Facebookcontexts.find(v=>v.From==sender_psid)==undefined)) is getting checked and for that array length is 1 all the time
I will look for some help from you guys.
Thanks ins advance
I need your help about CouchDB reduce function.
I have some docs like:
{'about':'1', 'foo':'a1','bar':'qwe'}
{'about':'1', 'foo':'a1','bar':'rty'}
{'about':'1', 'foo':'a2','bar':'uio'}
{'about':'1', 'foo':'a1','bar':'iop'}
{'about':'2', 'foo':'b1','bar':'qsd'}
{'about':'2', 'foo':'b1','bar':'fgh'}
{'about':'3', 'foo':'c1','bar':'wxc'}
{'about':'3', 'foo':'c2','bar':'vbn'}
As you can seen they all have the same key, just the values are differents.
My purpse is to use a Map/Reduce and my return expectation would be:
'rows':[ 'keys':'1','value':{'1':{'foo':'a1', 'at':'rty'},
'2':{'foo':'a2', 'at':'uio'},
'3':{'foo':'a1', 'at':'iop'}}
'keys':'1','value':{'foo':'a1', 'bar','rty'}
...
'keys':'3','value':{'foo':'c2', 'bar',vbn'}
]
Here is the result of my Map function:
'rows':[ 'keys':'1','value':{'foo':'a1', 'bar','qwe'}
'keys':'1','value':{'foo':'a1', 'bar','rty'}
...
'keys':'3','value':{'foo':'c2', 'bar',vbn'}
]
But my Reduce function isn't working:
function(keys,values,rereduce){
var res= {};
var lastCheck = values[0];
for(i=0; i<values.length;++i)
{
value = values[i];
if (lastCheck.foo != value.foo)
{
res.append({'change':[i:lastCheck]});
}
lastCheck = value;
}
return res;
}
Is it possible to have what I expect or I need to use an other way ?
You should not do this in the reduce function. As the couchdb wiki explains:-
If you are building a composite return structure in your reduce, or only transforming the values field, rather than summarizing it, you might be misusing this feature.
There are two approaches that you can take instead
Transform the results at your application layer.
Use the list function.
Lists functions are simple. I will try to explain them here:
Lists like views are saved in design documents under the key lists. Like so:
"lists":{
"formatResults" : "function(head,req) {....}"
}
To call the list function you use a url like this
http://localhost:5984/your-database/_design/your-designdoc/_list/your-list-function/your-view-name
Here is an example of list function
function(head, req) {
var row = getRow();
if (!row){
return 'no ingredients'
}
var jsonOb = {};
while(row=getRow()){
//construct the json object here
}
return {"body":jsonOb,"headers":{"Content-Type" : "application/json"}};
}
The getRow function is of interest to us. It contains the result of the view. So we can query it like
row.key for key
row.value for value
All you have to do now is construct the json like you want and then send it.
By the way you can use log
to debug your functions.
I hope this helps a little.
Apparently now you need to use
provides('json', function() { ... });
As in:
Simplify Couchdb JSON response
Hello I am trying to create a list of json objects in groovy
List relClinicStatementList = []
for (BloodTestRow row in BTList){
def jsonListBuilder = new groovy.json.JsonBuilder()
def internalJson = jsonListBuilder{
'targetRelationshipToSource' {
'code' 'part-of'
'codeSystem' 'MG'
}
'observationResult'{
'observationFocus'{
'code' "${row.exam}"
'codeSystem' 'mobiguide'
'displayName' "${row.exam}"
}
'observationValue' {
'physicalQuantity' {
'value' "${row.value}"
'unit' "${row.unit}"
}
}
}
}
println jsonListBuilder.toPrettyString()
relClinicStatementList.add(internalJson)
}
And the toPrettyString() method correctly shows the json structure I want.
However if at the end of the loop I try to print all of the items I have in the list like this:
for (JsonBuilder entry in relClinicStatementList){
println entry.toPrettyString()
}
I get all the elements inside my relClinicalStatement list to be equal to the latest I created... I felt like declaring a new JsonBuilder at each loop would prevent this behaviour... am I missing something? I must admit I come from Java and have the feeling that using groovy classes here makes this behave a little differently from what I expect.
How do I solve this issue?
Thanks in advance
I can't reproduce the behaviour you are seeing, but I think the problem is that I don't believe internalJson is what you think it is (it's a list of 2 closures).
If you change your code to:
List relClinicStatementList = btList.collect { row ->
new groovy.json.JsonBuilder( {
targetRelationshipToSource {
code 'part-of'
codeSystem 'MG'
}
observationResult {
observationFocus {
code "$row.exam"
codeSystem 'mobiguide'
displayName "$row.exam"
}
observationValue {
physicalQuantity {
value "$row.value"
unit "$row.unit"
}
}
}
} )
}
relClinicStatementList.each { entry ->
println entry.toPrettyString()
}
Does it work as you'd expect?
I am having issues with retrieving list items from a different site collection. I don't have issues when trying to recieve list items from within my current site collection. For example, http://myintranet.com/Departments/IT works. But http://myintranet.com/sites/Departments/IT returns an error.
if (!String.IsNullOrEmpty(SiteName) && !String.IsNullOrEmpty(SPContext.Current.Web.Url))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite intranetSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb currentWeb = intranetSite.AllWebs["/sites/projects/Physics"])
{
SPList postList = currentWeb.Lists.TryGetList("Issues");
if (postList != null)
{
IssueList.DataSource = postList.Items.GetDataTable();
IssueList.DataBind();
}
}
}
});
}
I haven't used any different code to what I normally do when trying to recieve list items. The only difference is that this time I getting list items from another site collection.
Thanks for any help!
The problem is intranetSite.AllWebs. This will only get SPWeb objects under your current site collection.
You cannot infer another site collection directly from one site collection.
Even though /sites/projects LOOKS like chid site collection from /, it;s not. /sites is just a managed path. / and /sites/projects are at the same level of the site collection hierarchy.
What you need to do is to this:
if (!String.IsNullOrEmpty(SiteName) && !String.IsNullOrEmpty(SPContext.Current.Web.Url))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPWeb currentWeb = new SPSite("http://server/sites/projects/Physics").OpenWeb())
{
SPList postList = currentWeb.Lists.TryGetList("Issues");
if (postList != null)
{
IssueList.DataSource = postList.Items.GetDataTable();
IssueList.DataBind();
}
}
});
}