await search in NodeJS
This is a very simple question as I'm new to both JS and cloudinary.
I got the following code working:
cloudinary.v2.search .expression('resource_type:image AND tags=kitten AND uploaded_at>1d AND bytes>1m') .sort_by('public_id','desc') .max_results(30) .execute() .then(result=>console.log(result));
Yet I want to put it into a function and return the search result, which seems impossible for me to do within the `then` function.
This the best I've come up with so far:
const searchImages = async (folder) => {
return await cloudinary.v2.search
.expression('folder:${folder}')
.execute()
.then((result) => {resolve(result)})
}
Yet, it is not working. I got
The promise rejected with the reason "#<Object>".] {
code: 'ERR_UNHANDLED_REJECTION'
}
This might be a very simple / sill syntax issue, but I just cannot fix it myself.
Please help.
-
Hi,
Adding a catch() to handle the error will help you to see what exceptions your code is encountering. And with that, the thrown exception is because it is required to use a backtick (`) when using the variable ${folder} when concatenating it with a string value.
For example:
var cloudinary = require('cloudinary').v2;
cloudinary.config({
cloud_name: 'your_cloud_name',
api_key: 'your_api_key',
api_secret: 'your_api_secret',
secure: true
});
const searchImages = async (folder) => {
let promise = new Promise((resolve, reject) => {
cloudinary.search
.expression(`folder:${folder}`)
.execute()
.then((result) => { resolve(result) })
.catch((error) => {
console.error(error)
});
});
let result = await promise; // wait until the promise resolves (*)
return result;
//console.log(result); // Admin API search results
}
searchImages('targetFolder').then((returnedValue) => {
console.log(returnedValue);
});1 -
thanks for the reply, without which I wasn't able to make the break through!
THANKS!
0
Post is closed for comments.
Comments
2 comments