I am unable to set response after create
hook.result.thumbnail = res._id
the process is like following:-
1) i am using fs model to upload video
2) after upload i am creating thumbnail by using "ThumbnailGenerator" model in hook :after create
3)in this i make another entry in mongodb for thumbnail , after insertion i take the id of that and patch in mongodb of video file
4) and then set the
hook.result.thumbanil = res._id
but this is not working
module.exports = (options = {}) => {
return async function (hook) {
var path = require('path'), // Default node module
pathToFile = path.join(__dirname, '../../fs/video/', hook.data.fileName),
pathToSnapshot = path.join(__dirname, '../../fs/image');
if (hook.data.type === "video") {
const ThumbnailGenerator = require('video-thumbnail-generator').default;
const tg = new ThumbnailGenerator({
sourcePath: pathToFile,
thumbnailPath: pathToSnapshot,
// tmpDir: pathToSnapshot //only required if you can't write to /tmp/ and you need to generate gifs
});
await tg.generateOneByPercentCb(90, (err, result) => {
const fsmodel = hook.app.services.fs
var promise1 = new Promise(async function (resolve, reject) {
let thumbnaildata = await fsmodel.create({ "type": "image", "extension": "jpg ", "fileName": result, "owner": hook.result.owner })
if (thumbnaildata) {
// resolve( thumbnaildata._id )
let update = await fsmodel.patch({ "_id": hook.result._id }, { "thumbnail": thumbnaildata._id })
if (update)
resolve(update)
else
reject("problem in patch")
}
});
promise1.then(function (res) {
hook.result.thumbnail = res.thumbnail
hook.data.thumbnail = res.thumbnail
// hook.result = res
//hook.data = res
console.log(hook.data)
return hook;
})
});
//return hook
}//end of if video
else {
return hook;
}
//return hook
}
}
You are using Promises incorrectly, the hook execution will not wait until it is completed. Using promisify and async/await all the way the code should be easier to follow:
const path = require('path');
const { promisify } = require('util');
module.exports = (options = {}) => {
return async function (hook) {
const pathToFile = path.join(__dirname, '../../fs/video/', hook.data.fileName);
const pathToSnapshot = path.join(__dirname, '../../fs/image');
if (hook.data.type === 'video') {
const ThumbnailGenerator = require('video-thumbnail-generator').default;
const tg = new ThumbnailGenerator({
sourcePath: pathToFile,
thumbnailPath: pathToSnapshot
});
const generateOneByPercent = promisify(tg.generateOneByPercentCb.bind(tg));
const result = await generateOneByPercent(90);
const fsmodel = hook.app.service('fs');
const thumbnaildata = await fsmodel.create({
type: 'image',
extension: 'jpg',
fileName: result,
owner: hook.result.owner
});
if (thumbnaildata) {
const res = await fsmodel.patch({ '_id': hook.result._id }, { 'thumbnail': thumbnaildata._id });
// You should only modify result or data, not both
hook.result.thumbnail = res.thumbnail;
// hook.data.thumbnail = res.thumbnail
}
}
return hook;
};
};
First thanks Daffl for reply
i used your correction , but i am getting this error
promisify is not a function
info: Feathers application started on http://localhost:3030
error: message=promisify is not a function, stack=TypeError: promisify is not a function
at Object.
at promise.then.hookObject (/Users/sanmeetsingh/sanmeet/backup_api/COPY/node_modules/@feathersjs/commons/lib/hooks.js:167:73)
at
Oops, it's const { promisify } = require('util');. I also can't guarantee that this will work out of the box but it should give you some pointers on how to put together your asynchronous code in order for it to do what you want.
yes it works for me ,thanks again
but can you explain how promisify work .
before above code i also write in below mention way ,just like normal
"const promise1 = async () => "
es6 promise work but not get any success
//////////////////////////////////////////////////////////////////
module.exports = (options = {}) => {
return async function (hook) {
//if (hook.type !== 'after') {
// throw new Error(`The 'checksubscribe' hook should only be used as a 'after' hook.`);
//}
var path = require('path'), // Default node module
pathToFile = path.join(__dirname, '../../fs/video/', hook.data.fileName),
pathToSnapshot = path.join(__dirname, '../../fs/image');
hook.result.thumbnail = "";
if( hook.result.type === "video"){
//import ThumbnailGenerator from 'video-thumbnail-generator';
const ThumbnailGenerator = require('video-thumbnail-generator').default;
const tg = new ThumbnailGenerator({
sourcePath: pathToFile,
thumbnailPath: pathToSnapshot,
// tmpDir: pathToSnapshot //only required if you can't write to /tmp/ and you need to generate gifs
});
// tg.generateOneByPercent(90)
// .then(console.log);
await tg.generateOneByPercentCb(90, (err, result) => {
if(err){
console.log("error in creating thumbnail",err);
}
else{
const fsmodel = hook.app.services.fs
const promise1 = async () => {
let thumbnaildata = await fsmodel.create({ "type":"image","extension":"png","fileName":result,"owner": hook.data.owner})
return thumbnaildata
}
promise1().then((res)=>{
hook.result.thumbnail = res._id
hook.data.thumbnail = res._id
return hook;
})
}
});
}//end of if video
else{
return hook;
}
}
}
You don't even need promisify. The package you are using already supports promises. So all you'd have to do is
const result = await tg.generateOneByPercent(90);
Thanks