FelipeJz
FelipeJz16mo ago

How to insert an array?

Just that, haven't found anything related.
6 Replies
Indy
Indy16mo ago
Just add an array as a field to a document you want to insert into your db. See: https://docs.convex.dev/database/types#convex-values and https://docs.convex.dev/database/writing-data#inserting-new-documents
Writing Data | Convex Developer Hub
Mutations can insert, update, and
Data Types | Convex Developer Hub
All Convex documents are defined as Javascript objects. These objects can have
FelipeJz
FelipeJzOP16mo ago
Sorry i wan't clear enough, what i wanted to do was insert many documents in a table at the same time, but already found a solution. Thanks
Nekeohloss
Nekeohloss4mo ago
Struggling with the same issue (inserting a document for each item in an array) and found this in search - any pointers on where to go with this? Current code:
objects.map(async (obj) => {
await ctx.db.insert("reports", { storageId: storageId, ...obj });
});
objects.map(async (obj) => {
await ctx.db.insert("reports", { storageId: storageId, ...obj });
});
objects is an array of objects who match the shape of my "reports" schema (minus the storageId that I'm inserting), and I'd like to create a new document for each object but when I try to insert them like this I'm getting a TS error: An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar, or explicitly marking the expression as ignored with the void operator.eslint@typescript-eslint/no-floating-promises
Hmza
Hmza4mo ago
@Nekeohloss you should wrap your map in a Promise.all. That way, it'll wait for all the inserts to complete before moving on try this: await Promise.all( objects.map((obj) => ctx.db.insert("reports", { storageId: storageId, ...obj }) ) );
Nekeohloss
Nekeohloss4mo ago
Ah that did it, thanks! I was so stuck on it being something particular to Convex that I missed what the message was actually saying 😅
Hmza
Hmza4mo ago
totally. happens to the best of us. glad its sorted!👍

Did you find this page helpful?