如何获取 Vimeo class 上传功能之外的回调值?
How to get the value of a callback outside the upload function of the Vimeo class?
我是 js 的新手,正在学习 Nestjs。我正在构建一个 API 来使用 Vimeo API 使用他们的 Vimeo 库。要上传视频,Vimeo class 有一个名为上传的方法:
upload(
file: string | File,
params: object,
completeCallback: UriCallback,
progressCallback: ProgressCallback | undefined,
errorCallback: ErrorCallback,
): void;
upload(
file: string | File,
completeCallback: UriCallback,
progressCallback: ProgressCallback | undefined,
errorCallback: ErrorCallback,
): void;
在我的服务中,我这样使用它:
async uploadVideo(@Body() video: UploadVideoDto): Promise<string> {
let videoUri: string;
this.client.upload(
video.pathToFile,
{
name: video.name,
description: video.description,
},
function (uri) {
console.log(uri);
videoUri = uri;
},
function (bytesUploaded, bytesTotal) {
console.log(bytesUploaded, bytesTotal);
},
function (error) {
throw new Error(error);
},
);
return videoUri;
}
function (uri) { console.log(uri) }
是上传完成时的回调函数,它给出了视频上传到的uri。我试着把它放在我的控制器上,像这样:
@Post('/upload')
async upload(@Body() video: UploadVideoDto) {
await this.appService.uploadVideo(video).then(function (uri): void {
console.log(uri);
});
}
问题是 console.log(uri) 总是打印未定义。我怎样才能从我的服务中获得回调 return 到我的控制器?
如果 uploadVideo
应该 return 解析为字符串的 Promise(uri
),它可能是这样的:
async uploadVideo(video: UploadVideoDto): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.client.upload(
video.pathToFile,
{
name: video.name,
description: video.description,
},
function (uri) {
resolve(uri)
},
function (bytesUploaded, bytesTotal) {
console.log(bytesUploaded, bytesTotal);
},
function (error) {
reject(error)
},
);
})
}
了解 JS async/await 功能。
我是 js 的新手,正在学习 Nestjs。我正在构建一个 API 来使用 Vimeo API 使用他们的 Vimeo 库。要上传视频,Vimeo class 有一个名为上传的方法:
upload(
file: string | File,
params: object,
completeCallback: UriCallback,
progressCallback: ProgressCallback | undefined,
errorCallback: ErrorCallback,
): void;
upload(
file: string | File,
completeCallback: UriCallback,
progressCallback: ProgressCallback | undefined,
errorCallback: ErrorCallback,
): void;
在我的服务中,我这样使用它:
async uploadVideo(@Body() video: UploadVideoDto): Promise<string> {
let videoUri: string;
this.client.upload(
video.pathToFile,
{
name: video.name,
description: video.description,
},
function (uri) {
console.log(uri);
videoUri = uri;
},
function (bytesUploaded, bytesTotal) {
console.log(bytesUploaded, bytesTotal);
},
function (error) {
throw new Error(error);
},
);
return videoUri;
}
function (uri) { console.log(uri) }
是上传完成时的回调函数,它给出了视频上传到的uri。我试着把它放在我的控制器上,像这样:
@Post('/upload')
async upload(@Body() video: UploadVideoDto) {
await this.appService.uploadVideo(video).then(function (uri): void {
console.log(uri);
});
}
问题是 console.log(uri) 总是打印未定义。我怎样才能从我的服务中获得回调 return 到我的控制器?
如果 uploadVideo
应该 return 解析为字符串的 Promise(uri
),它可能是这样的:
async uploadVideo(video: UploadVideoDto): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.client.upload(
video.pathToFile,
{
name: video.name,
description: video.description,
},
function (uri) {
resolve(uri)
},
function (bytesUploaded, bytesTotal) {
console.log(bytesUploaded, bytesTotal);
},
function (error) {
reject(error)
},
);
})
}
了解 JS async/await 功能。