从sequelize返回,然后返回JavaScript中的代码块

从sequelize返回,然后返回JavaScript中的代码块,javascript,node.js,json,orm,sequelize.js,Javascript,Node.js,Json,Orm,Sequelize.js,我试图从Sequelize的then块返回一个元素数组,但是输出是不可预测的。它的[目标承诺] 这是我试图运行的代码 const trips = ServiceProviderTrip.findAll({ where: { locationId: location, userId: UserID }, }) .then((result2) => {

我试图从Sequelize的then块返回一个元素数组,但是输出是不可预测的。它的[目标承诺]

这是我试图运行的代码

                const trips = ServiceProviderTrip.findAll({
                    where: { locationId: location, userId: UserID },
                })
                    .then((result2) => {
                        //TODO: to get the tripid of those locationid having same
                        const trips = [];
                        const json2 = result2;
                        for (var i = 0; i < json2.length; i++) {
                            var obj = json2[i];
                            var tripId = obj.tripID;
                            trips.push(tripId);
                        }
                        return trips
                    })
                    .catch((error) => {
                        console.log(red.inverse.bold(error))
                    });
                console.log(chalk.yellow.inverse.bold(trips))
但我想要的是:

[1,2,3,4]

提前感谢您对我的解决和帮助。这是因为
trips
变量并没有等待结果

您的代码是同步的

const trips = ServiceProviderTrip.findAll({
    where: { locationId: location, userId: UserID },
}).then((result2) => {
    //TODO: to get the tripid of those locationid having same
    const trips = [];
    const json2 = result2;
    for (var i = 0; i < json2.length; i++) {
        var obj = json2[i];
        var tripId = obj.tripID;
        trips.push(tripId);
    }
    return trips
}).catch((error) => {
    console.log(red.inverse.bold(error))
});
// If you want to get result here you have to write Asynchronous code 
console.log(chalk.yellow.inverse.bold(trips))

绝对的天才,先生。
const trips = ServiceProviderTrip.findAll({
    where: { locationId: location, userId: UserID },
}).then((result2) => {
    //TODO: to get the tripid of those locationid having same
    const trips = [];
    const json2 = result2;
    for (var i = 0; i < json2.length; i++) {
        var obj = json2[i];
        var tripId = obj.tripID;
        trips.push(tripId);
    }
    return trips
}).catch((error) => {
    console.log(red.inverse.bold(error))
});
// If you want to get result here you have to write Asynchronous code 
console.log(chalk.yellow.inverse.bold(trips))
const getTrips = async ()=>{
    const trips = await ServiceProviderTrip.findAll({
        where: { locationId: location, userId: UserID },
    }).then((result2) => {
        //TODO: to get the tripid of those locationid having same
        const trips = [];
        const json2 = result2;
        for (var i = 0; i < json2.length; i++) {
            var obj = json2[i];
            var tripId = obj.tripID;
            trips.push(tripId);
        }
        return trips
    }).catch((error) => {
        console.log(red.inverse.bold(error))
    });
    console.log(chalk.yellow.inverse.bold(trips))
}

getTrips()
ServiceProviderTrip.findAll({
    where: { locationId: location, userId: UserID },
}).then((result2) => {
    //TODO: to get the tripid of those locationid having same
    const trips = [];
    const json2 = result2;
    for (var i = 0; i < json2.length; i++) {
        var obj = json2[i];
        var tripId = obj.tripID;
        trips.push(tripId);
    }
    console.log(chalk.yellow.inverse.bold(trips))
}).catch((error) => {
    console.log(red.inverse.bold(error))
});