Combine two expect statements that perform check on single json response - javascript

I'm writing a test to check that a function in my Node.js application which returns this JSON structure:
}
id: 1,
name: 'John Doe',
email: 'j#doe.com',
phone: '+123',
suppliers: [
{
id: 1,
name: 'Supplier1'
}
]
}
I have this expect:
expect(res.body.users[0]).to.be.an.instanceof(Object)
.that.includes.all.keys([
'id',
'name',
'suppliers',
]);
I also want to check there are details in suppliers. I could just add this in another expect:
expect(res.body.users[0].suppliers[0]).to.be.an.instanceof(Object)
.that.includes.all.keys([
'id',
'name',
]);
Is it possible to combine both into one expect statement though?

Related

How can send data from an array in one embed message (discord.js)

I want to send data from an array in one embed message with few fields but my code sends them as 4 different embed messages with one field
I tried with this Code :
const pListEmbed = new Discord.MessageEmbed()
.setColor('#03fc41')
.setTitle('Connected')
.setDescription(Total : ${list.length})
.setThumbnail(config.logo)
.addFields(
array.flatMap(user => [
{ name: 'ID', value: user.id, inline: true },
{ name: 'Name', value: user.user_name, inline: true },
{ name: 'Identifier', value: user.identifier, inline: true }
])
)
)
.setTimestamp(new Date())
.setFooter('Used by: ' + message.author.tag, ${config.SERVER_LOGO});
message.channel.send(pListEmbed);
But it sends 4 embed messages with just one field that have id,username and identifier, and i don't want it like this. I want it to send one embed message with 4 different fields that have these 4 id,username and identifiers (we don't know how many of them we have)
Array :
[
{
id: '46892319372',
user_name: 'testerOne',
identifier: '20202'
}
]
[
{
id: '15243879678',
user_name: 'testerTwo',
identifier: '20201'
}
]
[
{
id: '02857428679',
user_name: 'testerThree',
identifier: '20203'
}
]
[
{
id: '65284759703',
user_name: 'testerFour',
identifier: '20204'
}
]
Your question is very unclear. If you want to send one embed for every user containing the ID, name and identifier, then you could do this:
list.forEach(user => {
const pListEmbed = new Discord.MessageEmbed()
.setColor('#03fc41')
.setTitle('Connected')
.setDescription(Total : ${list.length})
.setThumbnail(config.logo)
.setTimestamp(new Date())
.setFooter('Used by: ' + message.author.tag, ${config.SERVER_LOGO});
.addFields(
{
name: "ID"
value: user.id,
inline: true
},
{
name: "Name"
value: user.user_name,
inline: true
},
{
name: "Identifier"
value: user.identifier,
inline: true
}
)
message.channel.send(pListEmbed);
})
If on the other hand you want to send one embed only and it contains all the IDs, all the names, and all the identifiers, then this is impossible. Embed have character limits and since you don't know how many users you have, there could be thousands, thus busting the limit. And that would block you from sending the embed.
Also your "array" is in fact multiple arrays containing only one element each. I don't know if that's what you intended or not but it's not an array but multiple little ones.

How will I choose many objects with mongoose?

For example, I have
[
{ name: "John" },
{ name: "Mike" },
{ name: "Homer" },
{ name: "Bart" },
{ name: "Dmitry" },
{ name: "Dan" }
]
How will I choose many objects with mongoose, if I use .limit(2) I will get [{ name: "John" }, { name: "Mike" }], I need to choose [{ name: "Bart" }, { name: "Dmitry" }]. In default JS this method looks like .slice(3,5). How will I do it with mongoose?
You can try something like this:
Model.find({name: {$in: ["Bart", "Dmitry"]}});
You can achieve this in mongodb by using skip(3).limit(2).
In Mongoose you can achieve this with myModel.find({}, 'name', {skip: 3, limit: 2}), you just have to insert values of skip and limit you want in third parameter of find.
Here's documentation with an example of skip and here's a link to a more popular answer of similar problem.
Edit: Note that this is a short-sighted solution, you should use something else for large or changing database.

Sequelize Include Model But Without Tablename

I'm newbie for Sequelize
I have a problem with "Nested Eager Loading"
I have 2 Table with 1-to-many relationship
Comment Table
User Table
I use this code for the query
Comment.findAll({
include: [User]
})
I got
{
id: 1,
comment: "test",
user: {
id: 3,
name: "someone"
}
}
But I expected result like this
{
id: 1,
comment: "test",
user_id: 3,
user_name: "someone"
}
I read several stackoverflow post it has a solution but work for version 3.3
Comment.findAll({
attributes: ['id', 'name', ['user.id','user_id'], ['user.name','user_name']]
include: [{ model: User, attributes:[], nested: false, required: true }]
})
Comment.findAll({
attributes: ['id', 'name', [Sequelize.col('user.id'),'user_id'], [Sequelize.col('user.name'),'user_name']]
include: [{ model: User, attributes:[], nested: false, required: true }]
})
but it's not work for me.
now I use sequelize 5.5.1 how can I implement it.
Can someone help me, please?
You can use this code to get the desired output:
Since Sequelize is promised based , it is recommend to use then and catch.
Comment.findAll({
include: [User]
}).then(result=>
{
let obj=
{
id: result.item,
comment: result.comment,
user_id: result.user.id,
user_name: result.user.name
};
console.log(JSON.stringify(obj));
}).catch(err=>
{
console.log(err);
});

SEQUELIZE - How to Get All Assosciated Objects?

I have a many to many association like this:
Squad.belongsToMany(Member, {through: Membership});
Member.belongsToMany(Squad, {through: Membership});
How do I find all the Squads, and for each squad, show me the squad name and an array with the Members that each squad has associated with it?
UPDATED WITH WHAT I'VE BEEN TRYING AND WHAT RESULTS I GET:
I've been trying things like this:
Squad.findAll({
include: [{
model: Member,
required: true
}]
}).then(squads => {
// The rest of your logics here...
});
Here's a sample of what I get:
{ id: 3,
name: 'Knicks',
city: 'NYC',
state: 'NY',
'members.id': 3,
'members.name': 'Carmelo',
'members.city': 'NYC'
},
{ id: 3,
name: 'Knicks',
city: 'NYC',
state: 'NY',
'members.id': 2,
'members.name': 'Penny',
'members.city': 'Orlando',
'members.state': 'Florida'
}
But what I want is not multiples of the same object and individual member info. I'm trying to get something more like this:
{
id: 2,
name: 'Lakers',
members: [ memberInfo, memberInfo, memberInfo]
}
Is there a way to do that?
Assuming that you modeled your relations correctly, then you should be able to do something like
Squad.findAll({
include: [{
model: Member,
required: true
}]
}).then(squads => {
// The rest of your logics here...
});

Meteor: group documents and publish an object

I would like to know how to group documents according to a condition, and publish to the client.
Suppose I have the following documents:
[
{ name: 'John', createdAt: some_date_value_1 },
{ name: 'Jane', createdAt: some_date_value_2 },
{ name: 'Bob', createdAt: some_date_value_1 },
{ name: 'Jenny', createdAt: some_date_value_2 }
]
What can I do to publish a result like this?
{
some_date_value_1: [
{ name: 'John', createdAt: some_date_value_1 },
{ name: 'Bob', createdAt: some_date_value_1 }
],
some_date_value_2: [
{ name: 'Jane', createdAt: some_date_value_2 },
{ name: 'Jenny', createdAt: some_date_value_2 }
]
}
Any suggestions?
It depends of you want to do it on client or server.
First, the obvious solution: if you have no specific reason to store them with the first structure, store them directly using the second.
Second, another easy solution is to make in client. Here is a non tested code using undercore (bundled with meteor):
var yourCollection = CollectionName.find();
var yourCollectionByDate = _.groupBy(yourCollection , 'createdAt');
Third, you could still do it on server but either you will loose the reactivity of your collection (using for instance Collection.aggregate with a package) or have to transform and observe all changes afterwards (it would be a little overkill. However have a look here if you want more info)
A quick side note too: unless you want the users names to be unique, you should rely on mongo unique id (_.id) rather than on a name you set. That way, you are sure that you link to the right item (no duplicate).

Categories