mongoolia icon indicating copy to clipboard operation
mongoolia copied to clipboard

doc.pushToAlgolia is not a function

Open damiisdandy opened this issue 3 years ago • 0 comments

It throws this error when I run

User.syncWithAlgolia().then((r) => console.log(r)).catch((err) => console.log(err));

(node:3160) DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
POST /graphql 200 891.404 ms - 12130
TypeError: doc.pushToAlgolia is not a function
    at /Users/damiisdandy/Development/jetronmall/api/node_modules/mongoolia/dist/algolia-mongoose-model.js:44:22
    at Array.map (<anonymous>)
    at /Users/damiisdandy/Development/jetronmall/api/node_modules/mongoolia/dist/algolia-mongoose-model.js:43:32
    at Generator.next (<anonymous>)
    at step (/Users/damiisdandy/Development/jetronmall/api/node_modules/mongoolia/dist/algolia-mongoose-model.js:13:191)
    at /Users/damiisdandy/Development/jetronmall/api/node_modules/mongoolia/dist/algolia-mongoose-model.js:13:361
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

This is my model file

import { Schema, model } from 'mongoose';
import bcrypt from 'bcryptjs';
// @ts-ignore
import mongoolia from 'mongoolia';
import { algoliaConfig, PASSWORD_SALT_ROUNDS } from '../../config';

const User = new Schema(
  {
    firstName: { type: String, algoliaIndex: true },
    lastName: { type: String, algoliaIndex: true },
    phone: String,
    email: { type: String, algoliaIndex: true },
    password: String,
    isConfirmed: {
      type: Boolean,
      default: false,
    },
    roles: [{ type: Schema.Types.ObjectId, ref: 'Role' }],
  },
  { timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } }
);

User.pre('save', async function (next) {
  try {
    if (!this.isModified('password')) return next();
    // @ts-ignore
    this.password = await bcrypt.hash(this.password, PASSWORD_SALT_ROUNDS);
    next();
  } catch (err: any) {
    next(err);
  }
});

User.plugin(mongoolia, {
  ...algoliaConfig,
  indexName: 'users'
});

User.methods.validatePassword = async function (data) {
  // @ts-ignore
  return bcrypt.compare(data, this.password);
};

export default model('User', User);

but User.algoliaSearch works fine

damiisdandy avatar Jan 26 '22 12:01 damiisdandy