【发布时间】:2021-09-14 03:32:40
【问题描述】:
我正在使用 knex 创建动态删除功能。
编辑:该函数是从 GraphQL 突变中调用的,并且 tableName 和 whereClause 都是字符串。在下面的编辑代码中,我将覆盖 whereClause 以进行测试和演示。
export const deleteRecords = async (tableName, whereClause) => {
try {
// Edit: testing with json string
const whereClauseString = JSON.stringify('{id: 1000}');
console.log(whereClauseString); // outputs "{id: 1000}"
const whereClauseObject = JSON.parse(whereClauseString);
console.log(whereClauseObject);
console.log(whereClauseString); // outputs {id: 1000}
await connection
.from(tableName)
.where(whereClauseObject)
.del()
.then(count => {
const successMessage = `SUCCESS: ${count} records deleted from ${tableName}.`;
console.log(successMessage);
});
} catch (err) {
console.error(err);
}
};
但是,我收到此错误: “SyntaxError:JSON 中位置 1 的意外标记 i”
编辑:在传入的 json 字符串上使用 JSON.stringify 后,我现在收到以下错误
UnhandledPromiseRejectionWarning: TypeError: 运算符“未定义”是不允许的
由于某种原因,JSON.parse 似乎没有创建 knex 支持的对象
【问题讨论】:
-
您的 json 似乎无效。您是否尝试打印它并检查它是否是 jsonformatter.curiousconcept.com 之类的有效 json?