Node.js & JSON : 访问对象属性
Node.js & JSON : Accessing object properties
我正在使用 ExpressJS、NodeJS 和 Bookshelf.js
我正在尝试查看用户的朋友列表,但如果我想访问对象的 属性,我会收到 "Unhandled rejection TypeError: Cannot read property 'friends' of undefined"
错误,如下所示:friends[0].friends[0].email
router.get('/relative', function (req, res) {
user.where('id', 1).fetchAll({withRelated: ['friends']}).then(function (friends) {
//res.send(friends);
res.json(friends); // works
res.send(friends[0].friends[0].email); // throws error
});
});
如果我在客户端尝试相同的 JSON 对象(只是手动复制返回的 json 并粘贴到 fiddle),它会按预期工作。 jsfiddle在下面.
http://jsfiddle.net/tmrd/zjzs4etu
我做错了什么?
虽然 res.send(friends) 在上面给出了一个 JSON 对象,但是 console.log(friends) returns 这个 :
CollectionBase {
model:
{ [Function]
NotFoundError: [Function: ErrorCtor],
NoRowsUpdatedError: [Function: ErrorCtor],
NoRowsDeletedError: [Function: ErrorCtor] },
length: 1,
models:
[ ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: [Object],
cid: 'c2',
id: 1 } ],
_byId:
{ '1':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: [Object],
cid: 'c2',
id: 1 },
c2:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: [Object],
cid: 'c2',
id: 1 } },
_knex: null,
_events: {},
_eventsCount: 0 }
编辑 2
[ { id: 1,
email: 'user1@gmail.com',
username: 'user1',
password_hash: '',
image_id: null,
name: '',
is_public: 0,
language_id: null,
notification_quote: 1,
phone: '',
notify_sms_password_change: 0,
notify_sms_diff_country_login: 0,
notify_sms_unusual_activity: 0,
search_engine_visibility: 0,
allowed_senders: 1,
use_ssl: 0,
notify_mail_friendship_request: 1,
notify_mail_comment_on_my_status: 1,
notify_mail_comment_on_my_photo: 1,
notify_mail_birthdays: 1,
notify_mail_app_game_request: 1,
invite_number_of_people: '0',
sms_login: 0,
notify_sms_different_ip: 0,
allowed_friendship_requests: 1,
email_key: '',
points: 0,
confirmation_code: '',
confirmed: 0,
status: 1,
deleted_at: '0000-00-00 00:00:00',
cover_image_id: null,
updated_at: '0000-00-00 00:00:00',
created_at: '0000-00-00 00:00:00',
type: 0,
friends: [ [Object], [Object] ] } ]
编辑:我将从查询中排除敏感列,以便它们在生产中不可见,这只是为了举例。
好友集合需要是 json 字符串才能发送。
router.get('/relative', function (req, res) {
user.where('id', 1).fetchAll({withRelated: ['friends']}).then(function (friends) {
//res.send(friends);
friends = friends.toJSON();
res.json(friends); // res.json automatically calls the toJSON method if the function is no json
res.send(friends[0].friends[0].email);
});
});
我正在使用 ExpressJS、NodeJS 和 Bookshelf.js
我正在尝试查看用户的朋友列表,但如果我想访问对象的 属性,我会收到 "Unhandled rejection TypeError: Cannot read property 'friends' of undefined"
错误,如下所示:friends[0].friends[0].email
router.get('/relative', function (req, res) {
user.where('id', 1).fetchAll({withRelated: ['friends']}).then(function (friends) {
//res.send(friends);
res.json(friends); // works
res.send(friends[0].friends[0].email); // throws error
});
});
如果我在客户端尝试相同的 JSON 对象(只是手动复制返回的 json 并粘贴到 fiddle),它会按预期工作。 jsfiddle在下面.
http://jsfiddle.net/tmrd/zjzs4etu
我做错了什么?
虽然 res.send(friends) 在上面给出了一个 JSON 对象,但是 console.log(friends) returns 这个 :
CollectionBase {
model:
{ [Function]
NotFoundError: [Function: ErrorCtor],
NoRowsUpdatedError: [Function: ErrorCtor],
NoRowsDeletedError: [Function: ErrorCtor] },
length: 1,
models:
[ ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: [Object],
cid: 'c2',
id: 1 } ],
_byId:
{ '1':
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: [Object],
cid: 'c2',
id: 1 },
c2:
ModelBase {
attributes: [Object],
_previousAttributes: [Object],
changed: {},
relations: [Object],
cid: 'c2',
id: 1 } },
_knex: null,
_events: {},
_eventsCount: 0 }
编辑 2
[ { id: 1,
email: 'user1@gmail.com',
username: 'user1',
password_hash: '',
image_id: null,
name: '',
is_public: 0,
language_id: null,
notification_quote: 1,
phone: '',
notify_sms_password_change: 0,
notify_sms_diff_country_login: 0,
notify_sms_unusual_activity: 0,
search_engine_visibility: 0,
allowed_senders: 1,
use_ssl: 0,
notify_mail_friendship_request: 1,
notify_mail_comment_on_my_status: 1,
notify_mail_comment_on_my_photo: 1,
notify_mail_birthdays: 1,
notify_mail_app_game_request: 1,
invite_number_of_people: '0',
sms_login: 0,
notify_sms_different_ip: 0,
allowed_friendship_requests: 1,
email_key: '',
points: 0,
confirmation_code: '',
confirmed: 0,
status: 1,
deleted_at: '0000-00-00 00:00:00',
cover_image_id: null,
updated_at: '0000-00-00 00:00:00',
created_at: '0000-00-00 00:00:00',
type: 0,
friends: [ [Object], [Object] ] } ]
编辑:我将从查询中排除敏感列,以便它们在生产中不可见,这只是为了举例。
好友集合需要是 json 字符串才能发送。
router.get('/relative', function (req, res) {
user.where('id', 1).fetchAll({withRelated: ['friends']}).then(function (friends) {
//res.send(friends);
friends = friends.toJSON();
res.json(friends); // res.json automatically calls the toJSON method if the function is no json
res.send(friends[0].friends[0].email);
});
});