0

I'm building an API where a user can make a publication to be displayed on a thread. I'm trying to make the author data to be seen with the publication. This way the author data could be get like console.log( publication.author.completeName )

When saving publication, I save the author field with the value of the user id posting the publication. Then I'm trying to populate the data like shown here

This is my User model

const dynamoose = require("dynamoose");
const { v4: uuidv4 } = require('uuid');

const userSchema = new dynamoose.Schema(
  {
    id: {
      type: String,
      hashKey: true,
      default: () => uuidv4(),
    },
    email: {
      type: String,
      required: true
    },
    completeName: {
      type: String,
    },
    pseudo: {
      type: String, // Should make check on create and edit to ensure unicity of this column
    },
    gender: {
      type: String,
      enum: ['male', 'female', 'other']
    },
    speciality: {
      type: String
    },
    address: {
      type: String,
    },
    phoneNumber: {
      type: String,
    }
  },
  { timestamps: true }
);

module.exports = dynamoose.model("User", userSchema);

and this is my publication model:

const dynamoose = require("dynamoose");
const { v4: uuidv4 } = require('uuid');

const publicationSchema = new dynamoose.Schema(
  {
    id: {
      type: String,
      hashKey: true,
      default: () => uuidv4(),
    },
    photo: {
      type: Array,
      schema: [String],
      default: []
    },
    description: {
      type: String,
      required: true
    },
    anatomies: {
      type: Array,
      schema: [String],
      required: true,
    },
    specialities: {
      type: Array,
      schema: [String],
      required: true,
    },
    groupId: {
      type: String,
    },
    author: {
      type: String
    }
  },
  { timestamps: true }
);

module.exports = dynamoose.model("Publication", publicationSchema);

I'm trying to populate the author field when getting all the data like this:

exports.listPublication = async (req, res, next) => {
  try {
    Publication
      .scan()
      .exec()
      .then( async function (data) {
        return Promise.all( data.map(function(pub){
          return pub.populate({
            path: 'author',
            model: 'User'
          });
        }))
      })
      .then((data) => {
        success(res, { data: data });
      })
      .catch((err) => {
        throw new HttpException(err.message);
      });
  } catch (err) {
    error(next, res, err);
  }
}

but the author field is not populated, it only display the value of the author field, which is the string value of the author id.

Help please, I can't figure what I'm doing wrong

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Tiny Skillz
  • 191
  • 1
  • 1
  • 6

0 Answers0