Reputation: 333
I am querying the database for some information and I am using await-to-js
. But for some reason it is saying:
(node:28524) UnhandledPromiseRejectionWarning: TypeError: to is not a function
This is the website for reference:
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;
Upvotes: 0
Views: 257
Reputation: 122116
From the docs you linked to, under Usage:
import to from 'await-to-js';
// If you use CommonJS (i.e NodeJS environment), it should be:
// const to = require('await-to-js').default;
In non-code form (comment mine):
If you use CommonJS (i.e NodeJS environment), it should be:
const to = require('await-to-js').default; // ^------^ note
Upvotes: 2