While trying to save data from server side into MySql database, throws UnhandledPromiseRejectionWarning: TypeError:
While trying to save data from server side into MySql database, throws UnhandledPromiseRejectionWarning: TypeError:
在服务器端使用 Express
并使用 Axios
发送资源调用。在尝试将数据保存到 MySql 数据库时,我在控制台中遇到以下错误:使用 sequelize.js
进行 model/schema 创建。
错误:
(node:23364) UnhandledPromiseRejectionWarning: TypeError: this.build(...).save is not a function
at Function.create (C:\Main\Work\devchoice\node_modules\sequelize\lib\model.js:2228:8)
at CsvParser.<anonymous> (C:\Main\Work\devchoice\src\server.js:289:60)
at CsvParser.emit (events.js:315:20)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:23364) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch()
server.js
const manageNomineesSchema = require('./server/models/managenominees');
const ManageNomineesModel = manageNomineesSchema(sequelize, DataTypes);
app.post('/service/managenominees', upload.single('file'), async (req, res, next) => {
try {
if(req.file){
let filePath = req.file.path;
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', async () => {
console.log(results);
let manageNominees = await ManageNomineesModel.create(results);
//res.status(200).send(manageNominees);
res.status(200).json({ message: "Nominees inserted successfully !"});
});
}
} catch (e) {
res.status(500).json({ fail: e.message });
}
});
.create
将对象作为参数来创建 1 个实例,但是,您传递的是对象数组。
如果您想插入数组中的所有内容,请使用 .bulkCreate
。
参考:https://sequelize.org/master/class/lib/model.js~Model.html#static-method-bulkCreate
在服务器端使用 Express
并使用 Axios
发送资源调用。在尝试将数据保存到 MySql 数据库时,我在控制台中遇到以下错误:使用 sequelize.js
进行 model/schema 创建。
错误:
(node:23364) UnhandledPromiseRejectionWarning: TypeError: this.build(...).save is not a function
at Function.create (C:\Main\Work\devchoice\node_modules\sequelize\lib\model.js:2228:8)
at CsvParser.<anonymous> (C:\Main\Work\devchoice\src\server.js:289:60)
at CsvParser.emit (events.js:315:20)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:23364) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch()
server.js
const manageNomineesSchema = require('./server/models/managenominees');
const ManageNomineesModel = manageNomineesSchema(sequelize, DataTypes);
app.post('/service/managenominees', upload.single('file'), async (req, res, next) => {
try {
if(req.file){
let filePath = req.file.path;
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', async () => {
console.log(results);
let manageNominees = await ManageNomineesModel.create(results);
//res.status(200).send(manageNominees);
res.status(200).json({ message: "Nominees inserted successfully !"});
});
}
} catch (e) {
res.status(500).json({ fail: e.message });
}
});
.create
将对象作为参数来创建 1 个实例,但是,您传递的是对象数组。
如果您想插入数组中的所有内容,请使用 .bulkCreate
。
参考:https://sequelize.org/master/class/lib/model.js~Model.html#static-method-bulkCreate