Partial Update Object not working

I’m super new to Algolia, and I’m having an issue with the partialUpdateObject function. My app uses Prisma database, and everything updates there as expected. I’m also having no issues on the frontend, which is React if that’s helpful. My backend is Express. I apologize in advance for this cluster of a code block. I’ve tried so many variations of this that I’m out of ideas. I also tried—word for word—the example code that Algolia provides. Also, I’m able to sync up Algolia when I create a new record.

Within my Algolia dashboard, each record contains an Algolia-assigned objectID. When I console.log my object, the object comes back as undefined.

My code is really long because it contains the code to update Prisma as well, so I’ll first add the code with Prisma, followed by just the Algolia code, and then the error message. I very much appreciate any help! I’m 99% sure the rest of my function isn’t structured properly either (still getting familiar with async and promises).

With Prisma Code

const updateSandbox = async (req, res) => {
  const { id } = req.params;
  const {
    firstName,
    lastName,
    email,
    street,
    city,
    state,
    zip,
    phone,
    interests,
  } = req.body;

  const sandbox = await prisma.sandbox.update({
    where: {
      id,
    },
    data: {
      firstName,
      lastName,
      email,
      street,
      city,
      state,
      zip,
      phone,
      interests,
    },
  });

  res.status(200).json({ sandbox });

  const api_sandbox = {
    firstName: sandbox.firstName,
    lastName: sandbox.lastName,
    email: sandbox.email,
    street: sandbox.street,
    city: sandbox.city,
    state: sandbox.state,
    zip: sandbox.zip,
    phone: sandbox.phone,
    interests: sandbox.interests,
    id: sandbox.id,
    objectID: sandbox.objectID,
  };

  index
    .partialUpdateObject(api_sandbox, {
      createIfNotExists: true,
    }).catch((err) => {
      console.log(err);
    });
  console.log(api_sandbox)
};

Algolia Code Only

const api_sandbox = {
    firstName: sandbox.firstName,
    lastName: sandbox.lastName,
    email: sandbox.email,
    street: sandbox.street,
    city: sandbox.city,
    state: sandbox.state,
    zip: sandbox.zip,
    phone: sandbox.phone,
    interests: sandbox.interests,
    id: sandbox.id,
    objectID: sandbox.objectID,
  };

  index
    .partialUpdateObject(api_sandbox, {
      createIfNotExists: true,
    }).catch((err) => {
      console.log(err);
    });
  console.log(api_sandbox)

Error Message

{
name: 'ApiError',
   message: "The 'objectID' attribute is required to use action=partialUpdateObject near line:1 column:270",
   status: 400,
   transporterStackTrace: [
     {
       request: [Object],
       response: [Object],
       host: [Object],
       triesLeft: 3
     }
   ]
 }
1 Like

I just figured out how to fix this—I assigned the objectID to the sandbox.id and it worked like a charm.