Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用sequelize链接查询_Javascript_Sequelize.js - Fatal编程技术网

Javascript 使用sequelize链接查询

Javascript 使用sequelize链接查询,javascript,sequelize.js,Javascript,Sequelize.js,在sequelize中,我想做一些类似于链接findAll函数调用的事情 我想在这样的模型上做一个findAll const newYorkers = await People.findAll({ where: { city: 'New York' } }); 然后使用newYorkers作为函数的参数,在该函数上生成另一个findAll const get5ThAvenueInhabitants = (citiesInhabitants) => { return await cit

在sequelize中,我想做一些类似于链接findAll函数调用的事情

我想在这样的模型上做一个findAll

const newYorkers = await People.findAll({ where: { city: 'New York' } });
然后使用
newYorkers
作为函数的参数,在该函数上生成另一个findAll

const get5ThAvenueInhabitants = (citiesInhabitants) => {
  return await citiesInhabitants.findAll({ where: { street: '5th Avenue' } });
};

get5ThAvenueInhabitants(newYorkers);
这是行不通的,因为在第一次findAll之后,结果不再是一个模型,而是一个结果数组


sequelize有可能做到这一点吗?

如果你只想找到住在纽约和第五大道的人,为什么不使用
操作员

这将是如此简单


const newYorkers=People.findAll({
其中:{
城市:“纽约”,
街道:“第五大道”,
},
],
},
});

如果你只想找到住在纽约和第五大道的人,为什么不使用
接线员呢

这将是如此简单


const newYorkers=People.findAll({
其中:{
城市:“纽约”,
街道:“第五大道”,
},
],
},
});

首先,除了您的问题之外,我不理解函数
get5thavenueconlimitors()
背后的逻辑。如前所述,您可以直接使用
where:{city:'newyork',street:'5th Avenue'}
进行过滤。没有必要先询问“纽约”,然后再询问“第五大道”

现在回到您真正要问的问题,关于链接请求,您可以使用async/await,如下所示:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}
People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });
 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });

这样一来,
newYorkers
将等待所有数据被提取出来,然后继续
Get5ThavenueInumbients()

首先,除了你所问的之外,我不理解函数
Get5ThavenueInumbients()
背后的逻辑。如前所述,您可以直接使用
where:{city:'newyork',street:'5th Avenue'}
进行过滤。没有必要先询问“纽约”,然后再询问“第五大道”

现在回到您真正要问的问题,关于链接请求,您可以使用async/await,如下所示:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}
People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });
 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });

这样,
newYorkers
将等待直到所有数据都被提取出来,然后继续
get5thavenueincidents()

正如您所提到的,
People.findAll({where:{city:{newyork'})
返回
实例的数组
,而不是
模型
实例
对象确实公开了
销毁
更新
等方法,因此在某种意义上,您可以使用返回实例上的方法进行一种查询“链接”,如下所示:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}
People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });
 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });
要在应用程序代码中过滤返回的集合,而不是使用DB查询,可以执行以下操作:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}
People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });
 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });

正如你提到的,
People.findAll({where:{city:{newyork'}})
返回
实例的数组
,而不是
模型
实例
对象确实公开了
销毁
更新
等方法,因此在某种意义上,您可以使用返回实例上的方法进行一种查询“链接”,如下所示:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}
People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });
 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });
要在应用程序代码中过滤返回的集合,而不是使用DB查询,可以执行以下操作:

const mainFunction = async () => {
    const newYorkers = await People.findAll({ where: { city: 'New York' } });
    get5ThAvenueInhabitants(newYorkers);
}
People.findAll({ where: { city: 'New York' } }).then(people => {
     people.forEach(person => {
         person.destroy();
     })
 });
 People.findAll({ where: { city: 'New York' } }).then(people => {
     people.filter(person => {
        if(person.street === '5th Avenue'){
            return person;
        }
     })
 });

我想先过滤数据,然后再将其用作Get5Thavenue的参数。我不想给Get5ThavenueInumbients整个表。我想先过滤数据,然后再将它们用作Get5ThavenueInumbients的参数。我不想让Get5Thavenue占据整张桌子。是的,我忘记了我的示例中的等待。我现在就编辑了。但我觉得现在不行了。Get5ThavenueResidents中的findAll将在一个实例数组上调用。但是findAll只适用于模型。这就是我的意思,你实际上不能在
newYorkers
上调用
findAll
,你可以使用
filter
来实现这一点。当您可以在第一次查询时立即执行此操作时,这并不是必需的。是的,在我的示例中,我忘记了等待。我现在就编辑了。但我觉得现在不行了。Get5ThavenueResidents中的findAll将在一个实例数组上调用。但是findAll只适用于模型。这就是我的意思,你实际上不能在
newYorkers
上调用
findAll
,你可以使用
filter
来实现这一点。如果您可以在第一次查询时立即执行此操作,则不需要执行此操作。如果无法链接
findAll
s,则在下一次“查询”中使用
filter
的解决方案将是我的方法。但是我想知道
Array.filter
的性能是否会和
findAll
一样好。如果不可能链接
findAll
s,那么在下一个“查询”中使用
filter
的解决方案就是我的方法。但是我想知道
Array.filter
的性能是否会和
findAll
一样好。