与Knex.js同时使用req.body和SELECT语句
我正在使用Knex.js,express.js和body-parser设置Node.js API。
现在我想首先使用Insert into
request.body(我和postman atm这样做)
在第二手上另一个插入使用select语句,如下所示。
我已经连续尝试了2个knex.insert,但它只返回了第一个。在执行createQuestionnaire时,您认为我应该使用单独的ALTER TABLE语句来解决它吗?
table questionnaire
id,
title, (insert using req.body)
description, (insert using req.body)
created_by_id (fk) (insert using select-statement)
exports.createQuestionnaire = function (req, res) {
// The Code I need to implement
// knex('users').where({
// id: req.session.passport.user
// }).select('id')
//this works fine
knex
.insert(req.body)
.returning('*')
.into('questionnaire')
.then(function (data) {
res.send(data);
})
.catch(function (err) {
console.error(err);
res.set({ 'content-type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ message: "Failed" }));
});
};
我该如何解决?
回答如下:如果您需要根据其他查询结果执行查询,您可以链接多个承诺。
例如
exports.createQuestionnaire = function (req, res) {
knex('users').where({
id: req.session.passport.user
}).select('id').then(rows => {
if (!rows.length) {
throw 'user-not-found'
}
req.body['created_by_id'] = rows[0].id;
knex.insert(req.body) // Dangerous! You should check the required fields before inserting user input to db...
.returning('*')
.into('questionnaire')
.then(function (data) {
res.send(data);
})
.catch(function (err) {
console.error(err);
res.set({ 'content-type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ message: "Failed" }));
});
});
}
附:您可以尝试使用then & catch
而不是使用async & await
,您的代码将更具可读性。
与Knex.js同时使用req.body和SELECT语句
我正在使用Knex.js,express.js和body-parser设置Node.js API。
现在我想首先使用Insert into
request.body(我和postman atm这样做)
在第二手上另一个插入使用select语句,如下所示。
我已经连续尝试了2个knex.insert,但它只返回了第一个。在执行createQuestionnaire时,您认为我应该使用单独的ALTER TABLE语句来解决它吗?
table questionnaire
id,
title, (insert using req.body)
description, (insert using req.body)
created_by_id (fk) (insert using select-statement)
exports.createQuestionnaire = function (req, res) {
// The Code I need to implement
// knex('users').where({
// id: req.session.passport.user
// }).select('id')
//this works fine
knex
.insert(req.body)
.returning('*')
.into('questionnaire')
.then(function (data) {
res.send(data);
})
.catch(function (err) {
console.error(err);
res.set({ 'content-type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ message: "Failed" }));
});
};
我该如何解决?
回答如下:如果您需要根据其他查询结果执行查询,您可以链接多个承诺。
例如
exports.createQuestionnaire = function (req, res) {
knex('users').where({
id: req.session.passport.user
}).select('id').then(rows => {
if (!rows.length) {
throw 'user-not-found'
}
req.body['created_by_id'] = rows[0].id;
knex.insert(req.body) // Dangerous! You should check the required fields before inserting user input to db...
.returning('*')
.into('questionnaire')
.then(function (data) {
res.send(data);
})
.catch(function (err) {
console.error(err);
res.set({ 'content-type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ message: "Failed" }));
});
});
}
附:您可以尝试使用then & catch
而不是使用async & await
,您的代码将更具可读性。