未知的'json'值(部署 JavaScript 函数时)



我正在尝试部署一个从第三方网站获取值的Firebase函数(应用程序的通知(。但是,当我调用json时,它表示错误日志中不存在该值。有明显的解决方案吗?

后续编辑:如何使fetch方法中的数据显示在标题中?

const url = new URL(
"test.com"
);
let params = {
"value": "title",
};
Object.keys(params)
.forEach(key => url.searchParams.append(key, params[key]));
fetch(url, {
method: 'GET',
})
//This is where you usually would log the data .then(json => console.log(json));
const payload = {
notification: {
title: json,
body:  'The current events for today have been updated.',
badge: '0',
sound: 'default',
}
};

如果我理解您实际想要做什么,那么您就缺少了fetch请求的.then()方法。类似于:

fetch(url, {
method: 'GET'    // You also had a wrong comma here
}).then(json => {
// In here, json will contain the response from the fetch,
// so you can use it however you prefer
const payload = {
notification: {
title: json,
body:  'The current events for today have been updated.',
badge: '0',
sound: 'default',
}
// do anything else
});

您应该更新payload变量。没有称为json的变量作为title的值。你的payload应该是

const payload = {
notification: {
title: 'json', // <-- mark here
body:  'The current events for today have been updated.',
badge: '0',
sound: 'default',
}
};

更新:我误解了这个问题。我想你想要这样的东西-

const url = new URL(
"test.com"
);
let params = {
"value": "title",
};
Object.keys(params)
.forEach(key => url.searchParams.append(key, params[key]));
const payload = {
notification: {
title: '',
body:  'The current events for today have been updated.',
badge: '0',
sound: 'default',
}
};
fetch(url, {
method: 'GET',
}).then(res => res.json())
.then(data => {
payload.title = data.title; // Or something like that.
});