Not able to upsert using React Native fetch()

Hi there!

I’m trying to call your API with React Native’s fetch(), but I keep getting bad request errors.

public async upsertDocuments(documents: Document[]): Promise<void> {
    console.log('Upserting document ....');
    const records = documents.map((doc) => {
        const embedding = doc.embedding; // Note: embedding is number[]
        const id = String(doc.id);
        return {
            id: id,
            values: embedding,
        };
    });
    try {
        const request = {
            method: "POST",
            headers: {
                'Api-Key': this.apiKey,
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({vectors: records}),
            // Also tried: body: {vectors: records}
            // Also tried: body: {vectors: records, namespace: "ns1"}
            // Also tried: body: {'vectors': records},
    
        };
        const response = await fetch(`https://${this.indexHost}/vectors/upsert`, request);
        console.log(request);
        console.log(response);
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();
        console.log('Upserted document. Response:');
        console.log(data);
        // Handle the response data
    } catch (error) {
        console.error('Failed to upsert documents:', error);
    }
}

I get a “bad request” error.

Some things I’ve tried:

  • I’ve tried replacing the body with : JSON.stringify({vectors: [ { “id”: “vec1”, “values”: [0.1, 0.2, 0.3]}]}),

  • also tried removing JSON.stringify entirely. No matter what, each time I call the API I get a bad request error code 400.

  • Tried adding namespace too - same error (400).

  • Curl from the command line works.

  • Tried using the native library with TS, but I get an “Error: Object.assign is not a function” error.
    ( \node_modules@sinclair\typebox\typebox.js is where the error actually occurs) imported as part of '@pinecone-database/pinecone i think. This is what prompted me to use POST calls to begin with so as to not fight node js imports. :slight_smile:

Any idea what the problem might be with setting up my request? I tried curl from the command line and I’m able to call using the same parameters, so guessing it must be with how I’m setting up the call.

Thank you!

I followed Node.js fetch upsert stopped working without me changing anything - #8 by shore

const response = await fetch(request);
console.log(request);
console.log(response);
if (!response.ok) {
    const details = await response.text();
    console.log(details);
    throw new Error(`HTTP error! status: ${response.status}`);
}

and got this!: {“code”:3,“message”:“Vector dimension 1536 does not match the dimension of the index 512”,“details”:}

so… case closed!

I followed Node.js fetch upsert stopped working without me changing anything - #8 by shore

const response = await fetch(request);
console.log(request);
console.log(response);
if (!response.ok) {
    const details = await response.text();
    console.log(details);
    throw new Error(`HTTP error! status: ${response.status}`);
}

and got this!: {“code”:3,“message”:“Vector dimension 1536 does not match the dimension of the index 512”,“details”:}

so… case closed!

[Discourse post]