【发布时间】:2022-01-16 05:39:56
【问题描述】:
我正在开发一个类似于 Tinder 的约会应用程序,现在,我正在使用 node-express-mongodb-mongoose 服务器,但我遇到了问题。问题是,当应用程序加载时,在主页中,我想获取所有配置文件,不包括我的配置文件和我通过的配置文件(不喜欢)。下面是该函数在节点中的路由:
用户架构:
const userSchema = new mongoose.Schema({
email: {
type: 'string',
unique: [true, 'Email ID already present.'],
required: true,
},
password: {
type: 'string',
required: true,
minlength: 4,
},
displayName: {
type: 'string',
required: false,
default: '',
},
photoURL: {
type: 'string',
required: false,
default: '',
},
job: {
type: 'string',
required: false,
default: '',
},
age: {
type: 'number',
required: false,
default: null,
},
passes: {
type: 'Array',
required: false,
},
likes: {
type: 'Array',
required: false,
},
matches: {
type: 'Array',
required: false,
}
}
);
mongoose.model('User', userSchema);
获取配置文件路由:
const id = req.user._id;
try {
const currentUser = await User.findById({_id: id}); // gets the current user
const passes = currentUser.passes; // gets the passed/disliked profiles (array of objects)
const users = await User.find(); // gets all the registered users from db
console.log(passes);
const allUsers = [...passes, ...users]; // combining the 2 arrays
console.log('ALL USERS: ' + allUsers);
const usersAfterFilter = allUsers.filter(
(user) => user._id.toString() !== id.toString() // finally filtering because I want to fetch all the profiles excluding mine and the ones' which I have passed.
);
console.log('ALL USERS AFTER FILTER: ' + usersAfterFilter);
res.send({'response': 'success', 'data': usersAfterFilter});
} catch (error) {
console.log(error.message);
return res.status(422).send({response : error.message});
}
以下是日志输出:
[
[
{
_id: new ObjectId("612131375a7fd116969e298c"),
email: '',
password: '',
displayName: 'Yolo',
photoURL: '',
job: 'CEO of World',
age: 22,
passes: [],
likes: [],
matches: [],
createdAt: 2021-12-12T10:38:47.967Z,
updatedAt: 2021-12-12T10:39:14.943Z,
__v: 0
}
]
]
ALL USERS: [object Object],{
_id: new ObjectId("612131375a7fd116969e298c"),
email: '',
password: '',
displayName: 'Yolo',
photoURL: '',
job: 'CEO of World',
age: 22,
passes: [],
likes: [],
matches: [],
createdAt: 2021-12-12T10:38:47.967Z,
updatedAt: 2021-12-12T10:39:14.943Z,
__v: 0
},{
_id: new ObjectId("61b5d2395a7fd12347ae2994"),
email: '',
password: '',
displayName: 'your actor',
photoURL: '',
job: 'Actor',
age: 25,
passes: [ [ [Object] ] ],
likes: [],
matches: [],
createdAt: 2021-12-12T10:39:46.347Z,
updatedAt: 2021-12-12T10:41:36.627Z,
__v: 0
}
Cannot read properties of undefined (reading 'toString')
如您所见,当我将两个数组组合用于过滤目的时,...passes 转换为 [object Object],因此破坏了过滤的目的,因为我无法再根据 id 进行比较。
如果有人可以在这里帮助我,我将不胜感激!
【问题讨论】:
-
不是
...这样做,而是您在console.log中进行的字符串连接。'ALL USERS: ' + allUsers将allUsers转换为字符串,然后将其附加到"ALL USERS: ",并将其传递给console.log。 (我很惊讶地看到users对象似乎有特殊的toString处理,但passes对象显然没有,所以它们得到默认值,这导致"[object Object]"。)正确记录对象,将它们记录为自己的参数:console.log("ALL USERS:", allusers);. -
但是:与其使用
console.log手电筒在黑暗中四处游荡,不如使用IDE 中内置的调试器打开灯。在您希望调试器停止的语句上设置断点,运行代码,当调试器在断点处停止时,查看变量的值。 -
@TJCrowder 感谢您提供宝贵的反馈,我尝试调试并发现不知何故,每当我传递一个用户时,我认为它会将一个对象推入数组中,
passes: [ [Array] ],我没有不知道为什么要这样做,控制台中也没有错误,根据我的User架构,它应该可以正常工作...
标签: javascript node.js mongodb express mongoose