我得到' TypeError:无法将undefined或null转换为object'尝试访问nodejs中的json对象的长度。
以下是我的数据:
let colleges = JSON.parse(data)
第1步 - 将其解析为nodejs:
app.data.collegeData = data;
第2步 - 将其保存到对话框流程应用数据中:
let collegeLength = Object.keys(app.data.collegeData.college).length;
第3步 - 访问长度:
if ( app.data.collegeData === undefined ){
app.data.collegeData = [];
}
**Step 1 =>**
showColleges(college);
**Step 2 =>**
function showColleges(collegeName){
if (app.data.collegeData.length === 0){
getCollegeData().then(buildSingleCollegeResponse(collegeName))
.catch(function (err){
console.log('No college data')
console.log(err)
});
}
else{
buildSingleCollegeResponse(collegeName);
}
}
**Step 3 =>**
function getCollegeData(){
console.log('Inside get College Data')
return requestAPI(URL)
.then(function (data) {
let colleges = JSON.parse(data)
if (colleges.hasOwnProperty('college')){
saveData(colleges)
}
return null;
})
.catch(function (err) {
console.log('No college data')
console.log(err)
});
}
**Step 4 =>**
function saveData(data){
app.data.collegeData = data;
console.log(app.data.collegeData)
}
**Step 5 =>**
function buildSingleCollegeResponse(collegeName){
let responseToUser, text;
//console.log('Data is -> '+ Object.keys(app.data.collegeData.college).length);
//console.log('Length is -> '+ app.data.collegeData.college.length);
console.log('Count is -> '+app.data.collegeCount);
let collegeLength = Object.keys(app.data.collegeData.college).length;
if ( collegeLength === 0){
responseToUser = 'No colleges available at this time';
text = 'No colleges available at this time';
}
else if ( app.data.collegeCount < collegeLength ){
for ( var i = 1; i <= collegeLength; i++)
{
console.log('All Colleges:: '+app.data.collegeData.college[i])
let coll = app.data.collegeData.college[i]
let name = coll.name
console.log('checkCollegeExist => College Name:: '+ name)
console.log('checkCollegeExist => Parameter => College Name:: '+collegeName)
if(String(name).valueOf() === String(collegeName).valueOf()){
responseToUser = 'Yes! CSUF has '+collegeName;
text = 'Yes! CSUF has '+collegeName;
}else{
responseToUser = 'CSUF does not teach ' +collegeName+' currently';
text = 'CSUF does not teach ' +collegeName+' currently';
}
}
}
else{
responseToUser = 'No more colleges';
}
if (requestSource === googleAssistantRequest) {
sendGoogleResponse(responseToUser);
} else {
sendResponse(text);
}
}
在firebase控制台中出现以下错误:
TypeError:无法将undefined或null转换为object
更新:
以下是代码:
{{1}}&#13;
答案 0 :(得分:2)
这是罪魁祸首:
getCollegeData().then(buildSingleCollegeResponse(collegeName))
调用 buildSingleCollegeResponse(collegeName)
然后将其返回值传递到then
,就像foo(bar())
调用 bar
并将其返回值传递给foo
。
您想将功能传递给then
:
getCollegeData().then(() => buildSingleCollegeResponse(collegeName))
// An arrow function ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
请注意,现在从更新后的问题中可以清楚地看出app.data.collegeData.college
是数组,因此不需要Object.keys
。变化:
let collegeLength = Object.keys(app.data.collegeData.college).length;
简单地
let collegeLength = app.data.collegeData.college.length;
数组具有length
属性(默认情况下,非数组对象不具有)。
答案 1 :(得分:0)
在发出app.data
app.data.collegeData = data;
的值是多少?
如果未定义app.data
,您应该尝试app.data = {collegeData: data}
否则否定应用商店的存储。以下作品
app = {};
app.data = {};
app.data.collegeData = data;
app.data.collegeData.college.length
您不需要执行以下操作
let collegeLength = Object.keys(app.data.collegeData.college).length;