Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[求助]怎样使用js在获取mvHash的条件下获取mv的url #67

Open
xhzkp opened this issue Mar 5, 2025 · 16 comments
Open

[求助]怎样使用js在获取mvHash的条件下获取mv的url #67

xhzkp opened this issue Mar 5, 2025 · 16 comments

Comments

@xhzkp
Copy link

xhzkp commented Mar 5, 2025

Cannot read properties of undefined (reading 'backupdownurl')

使用下面的powershell代码可以完美的获取mv的url, 但是我用gpt把下面的代码转成js代码后, 始终报上面的错误, 对js不太懂,再次求助大佬, 先感谢.

$cookie = "token=???;userid=???"
$baseUrl = "http://localhost:3000"

$mvHash = '996EF3EEF3F09DE547C83C7E051E2BFB'
$response = Invoke-RestMethod -Uri "http://localhost:3000/video/url?hash=${mvHash}&cookie=${cookie}" -Method Get
$mvUrl = $response.data."$mvHash".backupdownurl
$mvUrl
@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

js的代码能不能发出来看一下

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

js的代码能不能发出来看一下

问gpt说是js没有powershell的语法糖: $response.data."$mvHash".backupdownurl 就是 ."$mvHash". 的写法

    const mvHash = songLists[0].MvHash;
    response = await axios.get(`${baseUrl}/video/url`, {
      params: {
        hash: mvHash,
        cookie: cookie
      }
    });
    const url = response.data[mvHash].backupdownurl;

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

你可以打印 response.data 出来看一下内容,有可能数据获取失败了

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

从错误来看,数据中不存在 backupdownurl 这个参数

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

你也可以把 const url = response.data[mvHash].backupdownurl; 这个改成 const url = response.data[mvHash]?.backupdownurl; ,这样不会报错,但url 有可能为空

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

你也可以把 const url = response.data[mvHash].backupdownurl; 这个改成 const url = response.data[mvHash]?.backupdownurl; ,这样不会报错,但url 有可能为空

试过了, 不行的, 那个是动态对象, powershell是动态语言, 那个语法糖是独家的

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

你可以打印 response.data 出来看一下内容,有可能数据获取失败了

有内容, 您使用powershell看一下那个内容就知道了, 它是根据hash动态生成的对象, 用当前最牛b的grok3, 对话了几十轮都没有解决

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

可以的,js也是支持,只要不是用ie浏览器

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

如果还报错改成 const url = response.data?.[mvHash]?.backupdownurl; 这样试一下

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

如果还报错改成 const url = response.data?.[mvHash]?.backupdownurl; 这样试一下

也不行,

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

行吧,完整一点的代码能不能发出来,等我有时间帮你看一下

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

行吧,完整一点的代码能不能发出来,等我有时间帮你看一下

const axios = require('axios');

const cookie = "token=???;userid=???";
const baseUrl = "http://localhost:3000";
const mvHash = '996EF3EEF3F09DE547C83C7E051E2BFB';

// 使用 async/await 的方式
async function getVideoUrl() {
    try {
        const response = await axios.get(`${baseUrl}/video/url?hash=${mvHash}&cookie=${cookie}`);
        const mvUrl = response.data.data[mvHash].backupdownurl;
        console.log(mvUrl);
        return mvUrl;
    } catch (error) {
        console.error('Error fetching video URL:', error);
    }
}

// 调用函数
getVideoUrl();

// 或者使用 Promise 的方式
/*
axios.get(`${baseUrl}/video/url?hash=${mvHash}&cookie=${cookie}`)
    .then(response => {
        const mvUrl = response.data.data[mvHash].backupdownurl;
        console.log(mvUrl);
    })
    .catch(error => {
        console.error('Error fetching video URL:', error);
    });
*/

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

行吧,完整一点的代码能不能发出来,等我有时间帮你看一下

大佬我是在n8n中使用的, 下面是n8n中代码节点中的js代码

const axios = require('axios');
return (async () => {
    const cookie = "token=xxx;userid=xxx";
    const baseUrl = "http://localhost:3000";
    const mvHash = '996EF3EEF3F09DE547C83C7E051E2BFB';

    try {
        // 使用 axios 进行请求
        const response = await axios.get(`${baseUrl}/video/url?hash=${mvHash}&cookie=${cookie}`);
        
        const mvUrl = response.data.data[mvHash].backupdownurl;
        
        // 返回 n8n 要求的数组格式
        return [{
            json: {
                mvUrl: mvUrl
            }
        }];
    } catch (error) {
        // 错误处理,返回错误信息
        return [{
            json: {
                error: error.message
            }
        }];
    }
})();

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

Image
大小写不一致导致获取失败

@MakcRe
Copy link
Owner

MakcRe commented Mar 5, 2025

const axios = require('axios');

const cookie = "token=???;userid=???";
const baseUrl = "http://localhost:3000";
const mvHash = '996EF3EEF3F09DE547C83C7E051E2BFB';

// 使用 async/await 的方式
async function getVideoUrl() {
    try {
        const response = await axios.get(`${baseUrl}/video/url?hash=${mvHash}&cookie=${cookie}`);
        const mvUrl = response.data.data[mvHash.toLowerCase()]?.backupdownurl;
        console.log(mvUrl);
        return mvUrl;
    } catch (error) {
        console.error('Error fetching video URL:', error);
    }
}

// 调用函数
getVideoUrl().then();

// 或者使用 Promise 的方式
/*
axios.get(`${baseUrl}/video/url?hash=${mvHash}&cookie=${cookie}`)
    .then(response => {
        const mvUrl = response.data.data[mvHash].backupdownurl;
        console.log(mvUrl);
    })
    .catch(error => {
        console.error('Error fetching video URL:', error);
    });
*/

改成 const mvUrl = response.data.data[mvHash.toLowerCase()]?.backupdownurl; 这样就行了

@xhzkp
Copy link
Author

xhzkp commented Mar 5, 2025

Image 大小写不一致导致获取失败

多谢大佬支招, powershell函数和变量不区分大小写, 看来js要严谨多了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants