I am using the Gmail API, where I get the messages sent to my account. Also using the Google Client libraries. The problem is that when printing the value of the object returned to the query it only does it if I print it as a variable. If I put it in an array and print it for some reason, it returns a different object.
This way the object is printed correctly:
let msg = gapi.client.request({
'path': 'https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages/'+msgId[0],
})
return msg
})
.then(function(msg){
console.log(msg)
})
Returned object:
body: "{\n \"id\": \"17befbfa6d794b69\",\n \"threadId\
headers: {cache-control: 'private', content-encoding: 'gzip', content-length: '6501', content-type: 'application/json; charset=UTF-8', date: 'Thu, 16 Sep 2021 18:49:18 GMT', …}
result: {id: '17befbfa6d794b69', threadId: '17befbfa6d794b69', labelIds: Array(3), snippet: 'Se ha concedido el acceso a tu cuenta de Google a …tu cuenta. Comprobar actividad También puedes ver', payload: {…}, …}
status: 200
statusText: null
Instead this way prints a different object that doesn't work for me:
let msgs = [];
let msg = gapi.client.request({
'path': 'https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages/'+msgId[0],
})
msgs.push(msg)
return msgs
})
.then(function(msgs){
console.log(msgs)
})
};
Returned Array:
> [$B]
0: $B
$a: UB
$W: false
Cf: null
QJ: false
Qk: "auto"
VL: false
Vg: {path: 'https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages/17befbfa6d794b69', method: 'GET', params: {…}, headers: {…}, body: undefined, …}
It was an async problem. The msg variable was saved as a promise instead of the value it was supposed to return. I solved it in the following way:
Dear friend, this dilemma is known as a syntax error. It is very common among users to ignore semicolons
;
.Basically your well-written code is as follows:
The code that follows is superfluous because it comes after the ``return`.
So the last thing to do with your code is to return the value of
msgs
, which is an array containing a single object, and that object is the promise not resulting from the request to the gapi client.I think I understand what your goal is, in any case, it should be like this: