arangodb / arangojs

The official ArangoDB JavaScript driver.
https://arangodb.github.io/arangojs
Apache License 2.0
601 stars 107 forks source link

AQL Query returns a Promise #507

Closed yuvanesh10 closed 6 years ago

yuvanesh10 commented 6 years ago

I have been trying to get a query result from Arangodb in to my front end service(Angular 4) using soap message. I am able to get a result of the query but printed out in console.log. But how can I get it under this function(myService). In other words, How can I feed my query result into a function rather than printing out the result in console. So that I can use this function to get the output of the query?

`server.js

   var myService = [          
            db.query(aqlQuery`
            LET startVertex = (FOR doc IN spec
            FILTER doc.serial_no == '"123456abcde"'
            LIMIT 2
            RETURN doc
            )[0]

            FOR v IN 1 ANY startVertex belongs_to
            RETURN v.ip`,
            {
            bindVar1: 'value',
            bindVar2: 'value',
            })
          ];

I would like to feed the function into soap msg and receive it Angular 4

 var soap_msg = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:examples:CheckUserNameService">' +
  '<soapenv:Header/>' +
 '<soapenv:Body>' +
 '<urn:CheckUserNameResponse 
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
 '<status xsi:type="xsd:string">' + (myService) + '</status>' +
 '</urn:CheckUserNameResponse>' +
 '</soapenv:Body>' +
'</soapenv:Envelope>';

But the output is either empty braces If I am using JSON.stringify or else it is [object Promise]. What am I doing wrong here ?

Version:
  "arangojs": "^5.8.0",
 "express": "^4.16.2",
"express-generator": "^4.15.5",
Simran-B commented 6 years ago

Already answered here: https://github.com/arangodb/arangodb/issues/4112#issuecomment-353068528

yuvanesh10 commented 6 years ago

@Simran-B If you dont mind me asking..,how can I await the result ? Even I used .then() but the result was same. I had something like this .then(function(response) { console.log(Retrieved documents., response._result); return res.status(200).json(response._result);

yuvanesh10 commented 6 years ago

@Simran-B @lodestone

I have tried in this way too

 db.query(aqlQuery`
 LET startVertex = (FOR doc IN spec
 FILTER doc.serial_no == '"123456abcde"'
 LIMIT 2
 RETURN doc
)[0]

 FOR v IN 1 ANY startVertex belongs_to
 RETURN v.ip`,
 {
 bindVar1: 'value',
 bindVar2: 'value',
 }
 ).then(function(res) {
          console.log("" + res._result);
          });

I tried to feed in the soap msg
'<status xsi:type="xsd:string">' + (res._result) + '</status>' +

and I am getting the ERROR:

ReferenceError: res is not defined

What am I doing wrong here ? I have been stuck at this point for a week .,Can someone help me figure it out ?

pluma commented 6 years ago

@yuvanesh10 It sounds like your problem is not related to arangojs but to a misunderstanding about how JavaScript promises work. I suggest you read this introduction first:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

I'm not sure where your SOAP code is in relation to the code you posted, but the error you're seeing is because the res argument is likely not in scope. Note that the function you pass to then is still a function and the same usual rules for scope apply:

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions

pluma commented 6 years ago

I'm closing this as it's not an arangojs question but a general JS question.

If you would like a more extensive introduction to JavaScript language features I would suggest checking out the (free) book series You Don't Know JS by Kyle Simpson, especially the book Async & Performance which covers async/await and promises:

https://github.com/getify/You-Dont-Know-JS#titles