plebbit / plebbit-js

A Javascript API to build applications using plebbit
GNU General Public License v2.0
41 stars 7 forks source link

TypeError: Cannot read properties of undefined (reading 'publishChallengeAnswers') on _vote.publishChallengeAnswers(challengeAnswers) #38

Closed windowshater closed 8 months ago

windowshater commented 8 months ago

Following the documentation:

const vote = await plebbit.createVote(createVoteOptions)
vote.on('challenge', async (challengeMessage, _vote) => {
  const challengeAnswers = await askUserForChallengeAnswers(challengeMessage.challenges)
  _vote.publishChallengeAnswers(challengeAnswers)
})
vote.on('challengeverification', console.log)
await vote.publish()

Uppon using the _vote.publishChallengeAnswers() function I'm getting the error:

file:///home/v/work/plebbit/plebbit-feeder/mySub.mjs:29
            _vote.publishChallengeAnswers(challengeAnswers);
                  ^

TypeError: Cannot read properties of undefined (reading 'publishChallengeAnswers')
    at Vote.<anonymous> (file:///home/v/work/plebbit/plebbit-feeder/mySub.mjs:29:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Looks like the _vote instance does not have that function, I avoid that by using the normal vote() function, here is my code:

import Plebbit from "@plebbit/plebbit-js";
import fs from "fs";
import readlineSync from "readline-sync";
async function test() {
    const options = {
        ipfsHttpClientsOptions: ["http://localhost:5001/api/v0"],
    };
    const plebbit = await Plebbit(options);
    plebbit.on("error", console.log);
    const signer = await plebbit.createSigner({
        privateKey: "...",
        type: "ed25519",
    });
    const vote = await plebbit.createVote({
        signer: signer,
        vote: 1,
        subplebbitAddress: "plebtoken.eth",
        commentCid: "QmbU2XsxFNd6pdfb6eCgz7iei3njQafzd8qHddNHo5kLXd",
    });
    try {
        console.log("getting challenge");
        vote.on("challenge", async (challengeMessage, _vote) => {
            console.log(_vote);
            console.log(challengeMessage);
            const challengeAnswers = await askUserForChallengeAnswers(
                challengeMessage.challenges[0].challenge
            );
            console.log(challengeAnswers);
            vote.publishChallengeAnswers(challengeAnswers);
        });
        vote.on("challengeverification", console.log);
        await vote.publish();
    } catch (e) {
        console.log(e);
    }
}
async function askUserForChallengeAnswers(challenge) {
    return new Promise(async (resolve) => {
        const timestamp = new Date().toISOString().replace(/[:.]/g, "_");
        const imagePath = `image_${timestamp}.txt`;
        const imageData = challenge.split(";base64,").pop();
        const imageBuffer = Buffer.from(imageData, "base64");
        fs.writeFileSync(imagePath, imageBuffer, "utf-8");
        const userInput = readlineSync.question(
            "Type the letters you see in the image: "
        );
        resolve([userInput]);
    });
}

test();
Rinse12 commented 8 months ago

That's because event challenge has only challengeMessage as a parameter, so _vote is undefined in your case.

This is the correct code

vote.on('challenge', async (challengeMessage) => {
  const challengeAnswers = await askUserForChallengeAnswers(challengeMessage.challenges)
  vote.publishChallengeAnswers(challengeAnswers)
})

@estebanabaroa should we remove instances of publications in challenge events? They're not needed because the original publication instance is already up-to-date