TypeError:无法读取 null 的属性“userID”
Posted
技术标签:
【中文标题】TypeError:无法读取 null 的属性“userID”【英文标题】:TypeError: Cannot read property 'userID' of null 【发布时间】:2019-04-06 00:30:23 【问题描述】:错误:
TypeError: 无法读取属性 'userID' of null
列出了我的UserDB.js
和profileController.js
,不确定这个错误是关于什么的。当我从我的页面转换并转到 myItems(cart) 时,它会吐出这个错误。
router.get('/myItems', function(req, res, next)
res.locals.signin = req.session.signin;
var UserDB = require('../models/UserDB');
var Users = UserDB.getUser();
console.log(Users);
var LocalUser = Users.find(function (ele) if (ele.userID === req.session.signin) return ele;);
if (SessionOn)
var UserProfiles = UserDB.GetUserProfiles();
var LocalProfile = UserProfiles.find(function (ele) if (ele.GetUserID() === LocalUser.userID) return ele;);
res.render('myItems', LocalUser: LocalUser, LocalProfile: LocalProfile, Session: res.locals.signin, SessionOn: SessionOn);
next()
);
-------
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/DB');
var Schema = mongoose.Schema;
var userDataSchema = new Schema(
userID: type: String, required: true,
password: type: String, required: true,
firstName: type: String, required: true,
lastName: type: String, required: true,
email: type: String, required: true,
city: type: String, required: true,
state: type: String, required: true,
zipCode: type: String, required: true
,collection: 'User');
var UserData = mongoose.model('User', userDataSchema);
function getAllUsers()
try
return UserData.find();
catch(e)
console.log(e);
function getUser(user)
try
return UserData.findOne(userID: user);
catch(e)
console.log("error " + e);
module.exports.getUser = getUser;
module.exports.getAllUsers = getAllUsers;
【问题讨论】:
【参考方案1】:您的猫鼬模型查询函数 Users.find() 为您提供了一个对象数组 因此,如果您想以您使用的方式访问它 试试这样
Users.findOne(userid:xxx,callback);
如果使用 find() 方法,则必须访问数组索引
Users.find(userId:xxx,(users)=>users[0]==neededId
【讨论】:
以上是关于TypeError:无法读取 null 的属性“userID”的主要内容,如果未能解决你的问题,请参考以下文章
TypeError:无法读取 null 的属性(读取“classList”)
TypeError:无法读取 null 的属性(读取“1”)
TypeError:无法读取 null 的属性“userID”