Javascript 检查promise中的JSON对象是否为null
我打开了另一个类似的话题,但得到了否定的回应。我在这里面临着一个合法的问题,我向你保证,我已经对此进行了广泛的研究。我相信我真正的问题可能是语法,因为我不是一个精通JS编码的人。这已经是令人尴尬的两周的持续斗争了。我有以下资料:Javascript 检查promise中的JSON对象是否为null,javascript,fetch-api,Javascript,Fetch Api,我打开了另一个类似的话题,但得到了否定的回应。我在这里面临着一个合法的问题,我向你保证,我已经对此进行了广泛的研究。我相信我真正的问题可能是语法,因为我不是一个精通JS编码的人。这已经是令人尴尬的两周的持续斗争了。我有以下资料: fetch('https://example.com/api/submission?data.dateOfService=' + data.dateChanger + '&select=data.endingPettyTotal') .then(respon
fetch('https://example.com/api/submission?data.dateOfService=' + data.dateChanger + '&select=data.endingPettyTotal')
.then(response => response.json())
.then(data => instance.setValue(data[0].data.endingPettyTotal));
我试图查看JSON对象,如果它的长度为零,请将我的data.enggpettytotal设置为“0”。如果不是零,则将其设置为JSON对象的值,如上面所述
似乎我应该能够通过检查response.json.length来完成这项工作,类似于下面的内容
fetch('https://example.com/api/submission?data.dateOfService=' + data.dateChanger + '&select=data.endingPettyTotal')
.then(response => {
if(response.json().length === 0) {
console.log("empty")
return 0;
}
else {
console.log("not empty")
return
})
.then(data => instance.setValue(data[0].Balance));
}
不幸的是,这似乎也不起作用。再一次,假设我刚刚弄乱了语法或其他东西,但是这个异步的东西又让人困惑。还假设这可以通过lodash isEmpty或isNull实现。您没有返回从第一个回调函数返回JSON的承诺 把支票放在第二个。然后 json返回与服务器发送的数据解析的承诺 那你就在第二天办理登机手续吧
fetch('https://example.com/api/submission?data.dateOfService=' + data.dateChanger + '&select=data.endingPettyTotal')
.then(response => {
if(!response.ok){
return Promise.resolve(0)// or just throw error
}
return response.json())
}).then(data =>
// if not array or empty array set as zero
const val = (Array.isArray(data) && data.length) ? data[0].Balance : 0;
instance.setValue(val);
})
response.json返回承诺对象,请尝试:
取回http://your.url'
.thenresponse=>response.json
.thendata=>instance.setValuedata.length&&data[0]。余额;
如果data.length等于0,则不希望设置值:
取回http://your.url'
.thenresponse=>response.json
.thendata=>data.length&&instance.setValuedata[0]。余额;
很难准确说出参数的名称。。。它们是服务器端的data.dateofService吗?假设下面是“服务日期” 通过json/FormData发送数据在PHP端有不同的处理方式。 发送json的另一种方法是新建FormData。。。并附加'key',value; 并将formData附加到body属性,而不是jsonData
const sendData = {
dateOfService: data.dateChanger,
select:data.endingPettyTotal
}
const jsonData = JSON.stringify(sendData);
const options = {
method:"POST",
body: jsonData
}
fetch('https://example.com/api/submission',options)
.then(response => response.json())
.then(data=>
{
if(data.length == 0) {
console.error("no data")
}
else {
console.log("not empty")
instance.setValue(data[0].Balance);
}
});
dateOfService实际上是我从表单中获取的值。我在select语句服务器端PHP中使用它来查询mySQL,然后发回与该日期相关联的“余额”。无论如何,我尝试了你的上述方法,效果很好。我刚刚添加了:instance.setValue'0';对于无数据if语句。非常感谢!
const sendData = {
dateOfService: data.dateChanger,
select:data.endingPettyTotal
}
const jsonData = JSON.stringify(sendData);
const options = {
method:"POST",
body: jsonData
}
fetch('https://example.com/api/submission',options)
.then(response => response.json())
.then(data=>
{
if(data.length == 0) {
console.error("no data")
}
else {
console.log("not empty")
instance.setValue(data[0].Balance);
}
});