WatermelonDB - 执行批量插入时出错
WatermelonDB - Error when performing batch insertion
我在我的 React Native Android 应用程序中在 WatermelonDB 上创建了一个 table。当我使用批处理从组件向 table 插入新记录时,我收到批处理内部抛出的错误 TypeError: Cannot read property 'id' of undefined
。
这是使用 Promise
个对象列表调用批处理的地方:
await database.action(async () => {
const allRecords = this.prepareInsertion(smsTransaction, records);
await database.batch(...allRecords);
console.log(allRecords.length);
});
prepareInsertion(smsTransaction, messages) {
return messages.map(async message => {
try {
return smsTransaction.prepareCreate(transaction => {
const parsedFields = parseFields(message);
transaction.type = parsedFields.type;
transaction.read_at = parsedFields.read_at;
});
} catch (e) {
console.log(e);
}
});
}
问题出在 async 关键字上。
return messages.map(message => {
要传递给 database.batch 的模型,而不是 Promises。
我在我的 React Native Android 应用程序中在 WatermelonDB 上创建了一个 table。当我使用批处理从组件向 table 插入新记录时,我收到批处理内部抛出的错误 TypeError: Cannot read property 'id' of undefined
。
这是使用 Promise
个对象列表调用批处理的地方:
await database.action(async () => {
const allRecords = this.prepareInsertion(smsTransaction, records);
await database.batch(...allRecords);
console.log(allRecords.length);
});
prepareInsertion(smsTransaction, messages) {
return messages.map(async message => {
try {
return smsTransaction.prepareCreate(transaction => {
const parsedFields = parseFields(message);
transaction.type = parsedFields.type;
transaction.read_at = parsedFields.read_at;
});
} catch (e) {
console.log(e);
}
});
}
问题出在 async 关键字上。
return messages.map(message => {
要传递给 database.batch 的模型,而不是 Promises。