I want to write a function that returns a Boolean indicating whether an image with the specified public_id already exists in my Cloudinary space.
I can log the result to the console with the following code:
function isUploaded(public_id) {
cloudinary.api.resource(public_id, function(response){
console.log(response.hasOwnProperty('public_id'));
});
};
isUploaded('test');
However, I want to pass on the result, the Boolean, to another function. Using a return statement results in { state: 'pending' } being logged:
function isUploaded(public_id) {
return cloudinary.api.resource(public_id, function(response){
return response.hasOwnProperty('public_id');
});
};
console.log(isUploaded('test'));
This is has something to do with javascript Promises. I can't seem to restructure my code to make it work though. Any help would be much appreciated.
The problem is that cloudinary.api.resource runs asynchronously (which is why it requires a callback function).
You can make your isUploaded function return a Promise that resolves once that callback is called.
var cloudinary = require('cloudinary');
function isUploaded(public_id) {
return new Promise(function (resolve, reject) {
cloudinary.api.resource(public_id, function(response) {
var isUploaded = response.hasOwnProperty('public_id');
resolve(isUploaded);
});
});
};
isUploaded('test')
.then(function (result) {
console.log(result);
})
Note that api.resource() is rate limited (part of the Admin API) so this is not a "scalable" solution.
You can perform a HEAD request and get the statusCode of the response - 200 means the resource exists in your account, 404 otherwise.
For example -
var http = require('http')
var options = {method: 'HEAD', host:'res.cloudinary.com',path:'/<cloud_name>/image/upload/<yourimage.jpg>'}
var req = http.request(options, function(res) {console.log(res.statusCode);});
req.end();
Related
I'm trying to make my own google action and I want to call an external api to get responses.
Here is my code:
const { conversation } = require('#assistant/conversation');
const functions = require('firebase-functions');
const app = conversation({debug:true});
const https = require('https');
app.handle('Tester', conv => {
// Implement your code here
conv.add("ok it works");
});
app.handle('Tester2', conv => {
// Implement your code here
let url = 'https://jsonplaceholder.typicode.com/users?_limit=2';
//const url = "https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";
http_req(url).then((message)=> {
console.log(message[0].name);
conv.add(message[0].name);
//return Promise.resolve();
});
});
function http_req(url) {
return new Promise((resolve, reject) => {
https.get(url, function(resp) {
var json = "";
resp.on("data", function(chunk) {
//console.log("received JSON response: " + chunk);
json += chunk;
});
resp.on("end", function() {
let jsonData = JSON.parse(json);
console.log(jsonData[0].name);
resolve(jsonData);
});
}).on("error", (err) => {
reject("Error: " + err.message);
});
});
}
exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);
The logs:
Error text:
Error: Response has already been sent. Is this being used in an async call that was not returned as a promise to the intent handler?
The problem is that the assistant won't say the conv.add(message[0].name); (obviusly it has a value)
Thanks in advance!
Thanks to a reddit user
https://www.reddit.com/r/GoogleAssistantDev/comments/lia5r4/make_http_get_from_fulfillment_in_nodejs/gn23hi3?utm_source=share&utm_medium=web2x&context=3
This error messages tells you just about all you need to know! Your
call to con.add() is indeed being used in an asynchronous call (the
callback chained to the Promise you created from http_req), and you
are indeed not returning that Promise.
Here's what's happening:
Google calls your 'Tester2' handler
You start an asynchronous HTTP request via http_req, encapsulated in a
Promise
Your function completes before the HTTP request does
Google sees that you are not returning anything from your handler and
assumes that you're done, so it sends the Response
The HTTP request finishes and its Promise resolves, calling your code
attached by the then() function
The simple solution here is to return the Promise created by your
http_req(...).then(...) code, so Google will know that you're not just
quite done, and it should wait for that Promise to resolve before
sending the Response.
If you can use async/await it becomes a bit clearer:
app.handle('Tester2', async conv => {
// Implement your code here
let url = 'https://jsonplaceholder.typicode.com/users?_limit=2';
//const url = "https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";
const message = await http_req(url);
console.log(message[0].name);
conv.add(message[0].name);
});
I would like to test my simple API that has /groups URL.
I want to make an API request to that URL (using Axios) before all tests begin and make the response visible to all test functions.
I am trying to make the response visible but not able to make it work. I followed a similar case with filling out the DB upfront but no luck with my case.
My simple test file below:
var expect = require('chai').expect
var axios = require('axios')
var response = {};
describe('Categories', function() {
describe('Groups', function() {
before(function() {
axios.get(config.hostname + '/groups').then(function (response) {
return response;
})
});
it('returns a not empty set of results', function(done) {
expect(response).to.have.length.greaterThan(0);
done();
})
});
});
I tried also a sligh modification of before function:
before(function(done) {
axios.get(config.hostname + '/groups')
.then(function (response) {
return response;
}).then(function() {
done();
})
});
but no luck too.
The error I am getting is simply that response isn't changing nor is visible within it. AssertionError: expected {} to have property 'length'
Summarising: How can I pass response from axios inside to in()?
Your first form is incorrect, because you're not returning the chained promise. As such, mocha has no way of knowing when your before is finished, or even that it's async at all. Your second form will solve this problem, but since axios.get already returns a promise, it's kind of a waste not to use mocha's built-in promise support.
As for making the response visible in the it, you need to assign it to a variable in a scope that will be visible within the it.
var expect = require('chai').expect
var axios = require('axios')
var response;
describe('Categories', function() {
describe('Groups', function() {
before(function() {
// Note that I'm returning the chained promise here, as discussed.
return axios.get(config.hostname + '/groups').then(function (res) {
// Here's the assignment you need.
response = res;
})
});
// This test does not need the `done` because it is not asynchronous.
// It will not run until the promise returned in `before` resolves.
it('returns a not empty set of results', function() {
expect(response).to.have.length.greaterThan(0);
})
});
});
I have a simple node Express app that has a service that makesa call to a node server. The node server makes a call to an AWS web service. The AWS simply lists any S3 buckets it's found and is an asynchronous call. The problem is I don't seem to be able to get the server code to "wait" for the AWS call to return with the JSON data and the function returns undefined.
I've read many, many articles on the web about this including promises, wait-for's etc. but I think I'm not understanding the way these work fully!
This is my first exposer to node and I would be grateful if somebody could point me in the right direction?
Here's some snippets of my code...apologies if it's a bit rough but I've chopped and changed things many times over!
Node Express;
var Httpreq = new XMLHttpRequest(); // a new request
Httpreq.open("GET","http://localhost:3000/listbuckets",false);
Httpreq.send(null);
console.log(Httpreq.responseText);
return Httpreq.responseText;
Node Server
app.get('/listbuckets', function (req, res) {
var bucketData = MyFunction(res,req);
console.log("bucketData: " + bucketData);
});
function MyFunction(res, req) {
var mydata;
var params = {};
res.send('Here are some more buckets!');
var request = s3.listBuckets();
// register a callback event handler
request.on('success', function(response) {
// log the successful data response
console.log(response.data);
mydata = response.data;
});
// send the request
request.
on('success', function(response) {
console.log("Success!");
}).
on('error', function(response) {
console.log("Error!");
}).
on('complete', function() {
console.log("Always!");
}).
send();
return mydata;
}
Use the latest Fetch API (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to make HTTP calls. It has built-in support with Promise.
fetch('http://localhost:3000/listbuckets').then(response => {
// do something with the response here
}).catch(error => {
// Error :(
})
I eventually got this working with;
const request = require('request');
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
parseString(body, function (err, result) {
console.log(JSON.stringify(result));
});
// from within the callback, write data to response, essentially returning it.
res.send(body);
}
else {
// console.log(JSON.stringify(response));
}
})
I am currently stumped with a problem, where the http.request() just fails/skips to make a request to a given url. I have the following setup
async.waterfall([
firstFunc,
secondFunc,
thirdFunc
], function last(err, result){});
The thirdFunc is where i am making a request and it looks like the following
function thirdFunc(payload, callback){
var responseFromCS = getData(payload);
callback(null, responseFromCS);
}
The getData function looks like the following
function getData(obj){
var curlttogetdata = csConstants.BasePath + csConstants.account_num;
var accountNum = obj.customer.accountnum;
var resData = null;
curlttogetdata = curlttogetdata.replace('${accntNum}', accountNum);
var getData = {
hostname: csURLHost,
path: curlttogetdata,
method:'GET',
headers: {
'X-consumer-id': csConstants.ConsumerIDHeader,
'Content-Type':'application/json'
}
};
var req = http.request(getData, function (res){
var body = '';
res.on('data', function getData(chunk){
body += chunk
});
res.on('end', function parseData(){
try {
resData = JSON.parse(body);
}catch(err){
resData = false;
}
});
});
req.on('error', function csResponseError(err){
resData = false;
});
req.end();
return resData;
}
Now upon debugging, once the debugger reaches http.request(...) it fails to step into callback or make the request and then steps right into req.end(). There is no error returned back. I have looked at my parameters in the getData object a number of times and everything looks fine. Even tested this with a curl and gives back the expected response.
one thing I see immediately is that you are returning resData as if it was a synchronous execution, meaning the httpRequest comes back after resData gets returned from your getData function, which will be null at that point
basically
when your program is executing
it does this
1 -makes http request,
2 -returns resData which is null (because the function executes until the end without stopping)
3 -the http request comes back and now resData has value but your function has already returned
what you need to do is pass a callback function
instead of
var responseFromCS = getData(payload);
you do getData(payload, function(responseFromCS){
//..... do something with the returned data
});
Ok so i believe i know the reasoning behind this. The 'http' node module is an async operation and the function getData that handles this, which is wrapped under thirdFunc in the async operation. I believe this operation gets executed before getData can respond.
So i moved the http.request() into thirdFunc and it works as expected.
I probably have some issues with the asyncness of Node.js.
rest.js
var Shred = require("shred");
var shred = new Shred();
module.exports = {
Request: function (ressource,datacont) {
var req = shred.get({
url: 'ip'+ressource,
headers: {
Accept: 'application/json',
},
on: {
// You can use response codes as events
200: function(response) {
// Shred will automatically JSON-decode response bodies that have a
// JSON Content-Type
if (datacont === undefined){
return response.content.data;
//console.log(response.content.data);
}
else return response.content.data[datacont];
},
// Any other response means something's wrong
response: function(response) {
return "Oh no!";
}
}
});
}
}
other.js
var rest = require('./rest.js');
console.log(rest.Request('/system'));
The problem ist if I call the request from the other.js I always get 'undefined'. If I uncomment the console.log in rest.js then the right response of the http request is written to the console. I think the problem is that the value is returned before the actual response of the request is there. Does anyone know how to fix that?
Best,
dom
First off, it is useful to strip down the code you have.
Request: function (ressource, datacont) {
var req = shred.get({
// ...
on: {
// ...
}
});
}
Your Request function never returns anything at all, so when you call it and console.log the result, it will always print undefined. Your request handlers for the various status codes call return, but those returns are inside of the individual handler functions, not inside Request.
You are correct about the asynchronous nature of Node though. It is impossible for you to return the result of the request, because the request will still be in progress when your function returns. Basically when you run Request, you are starting the request, but it can finish at any time in the future. The way this is handled in JavaScript is with callback functions.
Request: function (ressource, datacont, callback) {
var req = shred.get({
// ...
on: {
200: function(response){
callback(null, response);
},
response: function(response){
callback(response, null);
}
}
});
}
// Called like this:
var rest = require('./rest.js');
rest.Request('/system', undefined, function(err, data){
console.log(err, data);
})
You pass a third argument to Request which is a function to call when the request has finished. The standard Node format for callbacks that can fail is function(err, data){ so in this case on success you pass null because there is no error, and you pass the response as the data. If there is any status code, then you can consider it an error or whatever you want.