我正在查询数据库以获取某些信息,并且我正在使用await-to-js
。但是出于某种原因,它说:
(node:28524) UnhandledPromiseRejectionWarning: TypeError: to is not a function
这是供参考的网站:
https://www.npmjs.com/package/await-to-js
const {
User,
Connection,
SystemInfo,
LocationInfo
} = require('../../db/models');
var express = require('express');
var router = express.Router();
const to = require('await-to-js');
// Return complete user connections information
router.get('/:id_user', async (req, res) => {
const { id_user } = req.params;
let [error, users] = await to(
User.findOne({
where: { id_user },
include: [
{
attributes: ['id_connection', 'date'],
model: Connection,
include: [
{
attributes: ['browser'],
model: SystemInfo
},
{
attributes: ['country', 'city', 'ip'],
model: LocationInfo
}
]
}
]
})
);
if (error) {
// TODO: Log error
return res.status(502).json({ errorMessage: 'Some error', error });
}
console.log(user);
res.status(200).json(user);
});
module.exports = router;
答案 0 :(得分:2)
从您链接到的文档中,在Usage下:
const to = require('await-to-js').default;
// ^------^ note
以非代码形式(请注释我):
如果您使用CommonJS(即NodeJS环境),则应为:
try! String(contentsOf: URL(string: "https://www.google.com"), encoding: .utf16)