Json格式不正确

时间:2019-10-03 07:42:09

标签: javascript node.js json ajax mongoose

尝试从联系人集中的所有用户获取所有帖子时遇到问题。
第一。之后,我查询用户集合以获取用户ID。使用该用户ID从帖子集合中获取帖子。
在我的=>

PostController.js

// Get post status
let getPosts = await post.getPosts(req.user._id);

PostServices.js

let LIMIT_CONTACT = 3;
let getPosts = (currentUserId) => {
return new Promise(async (resolve, reject) => {
    let contacts = await ContactModel.getContacts(currentUserId, LIMIT_CONTACT);
    let getID = contacts.map(async contact => {
        if (contact.contactId == currentUserId) {
            let getUser = await UserModel.findNormalDataByID(contact.userId);
            return getUser;
        } else {
            let getUser = await UserModel.findNormalDataByID(contact.contactId);
            return getUser;
        }
    });

    let contactId = await Promise.all(getID);
    let getPostById = contactId.map(async contact => {
        return await PostModel.getPost(contact._id);
    });
    resolve(await Promise.all(getPostById));
});};

PostModel.js

    getAllPosts(currentUserId) {
        return this.find({ "postUserId": currentUserId }).sort({ "createdAt": -1 }).exec();
    },
    getPost(postID) {
        return this.find({ "postUserId": postID }).exec();
    },

这样的预期数据

[{ numOfLikes: 1,
  numOfComments: 0,
  status: 'public',
  editedAt: null,
  deletedAt: null,
  _id: 5d9551f793ad0609f82f397f,
  postUser: 'Hai Loc',
  postUserId: '5d46b7e5e08f4222384259b9',
  avatar:
   '1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
  postContent:
   'sample post content',    
  comments: [],
  createdAt: 1570066935792,
  __v: 0 },
{ numOfLikes: 2,
  numOfComments: 0,
  status: 'public',
  editedAt: null,
  deletedAt: null,
  _id: 5d95549693ad0609f82f3980,
  postUser: 'Hai Loc',
  postUserId: '5d46b7e5e08f4222384259b9',
  avatar:
   '1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
  postContent: 'create new post',
  comments: [Array],
  createdAt: 1570067606887,
  __v: 0 }]

但是这种类型带有多个方括号

[ [ { numOfLikes: 1,
      numOfComments: 0,
      status: 'public',
      editedAt: null,
      deletedAt: null,
      _id: 5d9551f793ad0609f82f397f,
      postUser: 'Hai Loc',
      postUserId: '5d46b7e5e08f4222384259b9',
      avatar:
       '1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
      postContent:
       'sample post content',    
      comments: [],
      createdAt: 1570066935792,
      __v: 0 },
    { numOfLikes: 2,
      numOfComments: 0,
      status: 'public',
      editedAt: null,
      deletedAt: null,
      _id: 5d95549693ad0609f82f3980,
      postUser: 'Hai Loc',
      postUserId: '5d46b7e5e08f4222384259b9',
      avatar:
       '1566978062948-2a5589c9-733f-48ff-9be4-d4f542946c1f-storm view.jpg',
      postContent: 'create new post',
      comments: [Array],
      createdAt: 1570067606887,
      __v: 0 } ],
  [],
  [],
  [],
  [],
  [],
  [],
  [],
  [],
  [] ]

请帮助,我对Promise.all做错了什么(json格式不正确)

0 个答案:

没有答案