Attempting to save data that is streamed from a twitter api - node.js

I am trying to save data from tweets to a mongoDB database using node and express.
I am using the twitter api to stream twitter data with a specific hashtags. I just want to save the text content of the post:
Here is how the tweet content shows up when it is console.logged:
(Note this feature works and this is my own posted
{
created_at: 'Tue Mar 15 06:38:58 +0000 2022',
id: 1503621761388134400,
id_str: '1503621761388134410',
text: '#TelecomDisaster Test for project 2',
source: 'Twitter Web App',
truncated: false,
in_reply_to_status_id: null,
in_reply_to_status_id_str: null,
in_reply_to_user_id: null,
in_reply_to_user_id_str: null,
in_reply_to_screen_name: null,
user: {
id: 1472188612494172200,
id_str: '1472188612494172172',
name: 'Dillon Rampersad',
screen_name: 'R_Dillon_25',
location: null,
url: null,
description: null,
translator_type: 'none',
protected: false,
verified: false,
followers_count: 5,
friends_count: 11,
listed_count: 0,
favourites_count: 22,
statuses_count: 63,
created_at: 'Sat Dec 18 12:55:26 +0000 2021',
utc_offset: null,
time_zone: null,
geo_enabled: false,
lang: null,
contributors_enabled: false,
is_translator: false,
profile_background_color: 'F5F8FA',
profile_background_image_url: '',
profile_background_image_url_https: '',
profile_background_tile: false,
profile_link_color: '1DA1F2',
profile_sidebar_border_color: 'C0DEED',
profile_sidebar_fill_color: 'DDEEF6',
profile_text_color: '333333',
profile_use_background_image: true,
profile_image_url: 'http://pbs.twimg.com/profile_images/1472188757956780033/OMlZZeZI_normal.jpg',
profile_image_url_https: 'https://pbs.twimg.com/profile_images/1472188757956780033/OMlZZeZI_normal.jpg',
default_profile: true,
default_profile_image: false,
following: null,
follow_request_sent: null,
notifications: null,
withheld_in_countries: []
},
geo: null,
coordinates: null,
place: null,
contributors: null,
is_quote_status: false,
quote_count: 0,
reply_count: 0,
retweet_count: 0,
favorite_count: 0,
entities: { hashtags: [ [Object] ], urls: [], user_mentions: [], symbols: [] },
favorited: false,
retweeted: false,
filter_level: 'low',
lang: 'en',
timestamp_ms: '1647326338513'
}
I want to save text: '#TelecomDisaster Test for project 2', and created_at: 'Tue Mar 15 06:38:58 +0000 2022', to my database.
I am trying with the function below to save just the text for now but i dont quite understand how to:
const express = require('express')
const router = new express.Router();
var Twitter = require('twit')
const TwitterPosts = require("../db/models/TwitterPosts.model");
//api keys goes here but it removed for safety
var stream = client.stream('statuses/filter', { track: '#TelecomDisaster' })
stream.on('tweet', function (tweet) {
console.log(tweet)
let newtweet = new TwitterPosts({
tweet: req.body.postContent
});
newtweet.save().then((twit) => {
res.send(twit);
console.log(twit);
})
});
module.exports = router;
The model for the schema:
const mongoose = require('mongoose');
const TwitterPostsSchema = new mongoose.Schema({
twitterUsername:{
type: String,
required: false,
minlength:1,
trim: true
},
postContent:{
type: String,
required: false,
minlength:1,
trim: true
},
postDateTime:{
type: Date,
required: false,
default: Date.now
}
})
const TwitterPosts = mongoose.model( 'TwitterPosts', TwitterPostsSchema);
module.exports = TwitterPosts
Whenever it trys to save i get the error
tweet: req.body.postContent
^
ReferenceError: req is not defined
i did not define req but in this use case i dont know how to do that exactly when streaming the tweets.
To conclude i am trying to save tweets to a mongoDB database using node and express. the tweets are streamed as shown above but i dont quite understand how it is saved to the database.

you receive tweet in stream.on listener, so it's just tweet, instead of req.body.postContent:
let newtweet = new TwitterPosts({
tweet: tweet
});
or, according to your schema:
let newtweet = new TwitterPosts({
twitterUsername: tweet.user.name,
postContent: tweet.text,
postDateTime: tweet.created_at
});

Related

Channel cache is incoherent

I've built a few functions to table channel & category information, one of which runs when the bot starts to make sure everything is synced.
The problem I've run into is that bot.channels.cache contains channels that no longer exist, or states of a channel that no longer exists. For example, I only have one channel in the server called "general". Yet, there are 3 separate entries for that channel by name, and only one contains the ID (711043006781849686) of the current "general" channel:
import Discord from 'discord.js'
import config from '../config.js'
const bot = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] })
bot.login(config.botToken)
bot.on('ready', async () => {
console.log(bot.channels.cache)
}
-- returns --
Collection(46) [Map] {
...
'711043006781849686' => <ref *16> TextChannel {
type: 'text',
deleted: false,
id: '711043006781849686',
name: 'general',
rawPosition: 10,
parentID: '711043007197216880',
permissionOverwrites: Collection(3) [Map] {
'711043006253367426' => [PermissionOverwrites],
'711043006295179347' => [PermissionOverwrites],
'861109585930747934' => [PermissionOverwrites]
},
topic: 'General chat channel.',
nsfw: false,
lastMessageID: '860794574707752980',
rateLimitPerUser: 0,
lastPinTimestamp: null,
guild: Guild {
members: [GuildMemberManager],
channels: [GuildChannelManager],
roles: [RoleManager],
presences: [PresenceManager],
voiceStates: [VoiceStateManager],
deleted: false,
available: true,
id: '711043006253367426',
shardID: 0,
name: 'Omegabox',
icon: null,
splash: null,
discoverySplash: null,
region: 'us-central',
memberCount: 5,
large: false,
features: [Array],
applicationID: null,
afkTimeout: 900,
afkChannelID: '711043009944223832',
systemChannelID: '711043006781849686',
embedEnabled: undefined,
premiumTier: 0,
premiumSubscriptionCount: 0,
verificationLevel: 'NONE',
explicitContentFilter: 'DISABLED',
mfaLevel: 0,
joinedTimestamp: 1589597389528,
defaultMessageNotifications: 'ALL',
systemChannelFlags: [SystemChannelFlags],
maximumMembers: 100000,
maximumPresences: null,
approximateMemberCount: null,
approximatePresenceCount: null,
vanityURLCode: null,
vanityURLUses: null,
description: null,
banner: null,
rulesChannelID: null,
publicUpdatesChannelID: null,
preferredLocale: 'en-US',
ownerID: '598729034867933195',
emojis: [GuildEmojiManager]
},
messages: MessageManager {
cacheType: [class LimitedCollection extends Collection],
cache: [LimitedCollection [Map]],
channel: [Circular *16]
},
_typing: Map(0) {}
},
...
'827343616678559757' => <ref *33> TextChannel {
type: 'text',
deleted: false,
id: '827343616678559757',
name: 'general',
rawPosition: 0,
parentID: '827343616678559755',
permissionOverwrites: Collection(0) [Map] {},
topic: null,
lastMessageID: '830245759152291860',
rateLimitPerUser: 0,
lastPinTimestamp: null,
guild: Guild {
members: [GuildMemberManager],
channels: [GuildChannelManager],
roles: [RoleManager],
presences: [PresenceManager],
voiceStates: [VoiceStateManager],
deleted: false,
available: true,
id: '827343616678559754',
shardID: 0,
name: 'Megabox Emojis 1',
icon: null,
splash: null,
discoverySplash: null,
region: 'us-west',
memberCount: 3,
large: false,
features: [],
applicationID: null,
afkTimeout: 300,
afkChannelID: null,
systemChannelID: '827343616678559757',
embedEnabled: undefined,
premiumTier: 0,
premiumSubscriptionCount: 0,
verificationLevel: 'NONE',
explicitContentFilter: 'DISABLED',
mfaLevel: 0,
joinedTimestamp: 1617380998194,
defaultMessageNotifications: 'ALL',
systemChannelFlags: [SystemChannelFlags],
maximumMembers: 100000,
maximumPresences: null,
approximateMemberCount: null,
approximatePresenceCount: null,
vanityURLCode: null,
vanityURLUses: null,
description: null,
banner: null,
rulesChannelID: null,
publicUpdatesChannelID: null,
preferredLocale: 'en-US',
ownerID: '598729034867933195',
emojis: [GuildEmojiManager]
},
messages: MessageManager {
cacheType: [class LimitedCollection extends Collection],
cache: [LimitedCollection [Map]],
channel: [Circular *33]
},
nsfw: false,
_typing: Map(0) {}
},
...
'827344454259703842' => <ref *34> TextChannel {
type: 'text',
deleted: false,
id: '827344454259703842',
name: 'general',
rawPosition: 0,
parentID: '827344454259703840',
permissionOverwrites: Collection(0) [Map] {},
topic: null,
lastMessageID: '827580681730261032',
rateLimitPerUser: 0,
lastPinTimestamp: null,
guild: Guild {
members: [GuildMemberManager],
channels: [GuildChannelManager],
roles: [RoleManager],
presences: [PresenceManager],
voiceStates: [VoiceStateManager],
deleted: false,
available: true,
id: '827344454259703838',
shardID: 0,
name: 'Megabox Emojis 2',
icon: null,
splash: null,
discoverySplash: null,
region: 'us-west',
memberCount: 3,
large: false,
features: [],
applicationID: null,
afkTimeout: 300,
afkChannelID: null,
systemChannelID: '827344454259703842',
embedEnabled: undefined,
premiumTier: 0,
premiumSubscriptionCount: 0,
verificationLevel: 'NONE',
explicitContentFilter: 'DISABLED',
mfaLevel: 0,
joinedTimestamp: 1617381010142,
defaultMessageNotifications: 'ALL',
systemChannelFlags: [SystemChannelFlags],
maximumMembers: 100000,
maximumPresences: null,
approximateMemberCount: null,
approximatePresenceCount: null,
vanityURLCode: null,
vanityURLUses: null,
description: null,
banner: null,
rulesChannelID: null,
publicUpdatesChannelID: null,
preferredLocale: 'en-US',
ownerID: '598729034867933195',
emojis: [GuildEmojiManager]
},
messages: MessageManager {
cacheType: [class LimitedCollection extends Collection],
cache: [LimitedCollection [Map]],
channel: [Circular *34]
},
nsfw: false,
_typing: Map(0) {}
}
}
I'm aware that there is a category named "General" (ID: 711043007197216880), but the casing is different which is maintained in an entry. I've filtered that one out, along with everything else that isn't "general" from the above block.
What am I missing here? Possible to sync things up?
Always have an epiphany right after finally making a post.
Been at this for hours and never realized that in this specific call I'm not specifying what guild, or rather what server. The bot is in multiple servers, and I've already got the current server's ID in the config I'm working with.
Changed bot.channels.cache to bot.guilds.cache.get(config.guildId).channels.cache.

stream.on('tweet') twitter Bot

i am facing a problem relating to twitter bot. i was simply trying to get the tweeets with '#topi' mentioned in their status and then send them a reply and also retweet the tweet.
this is how i expect to execute the bot.
wait for tweets
receives the tweet
write the tweet to a local file (just to examine)
reply the tweet with their user_id mentioned
retweet their tweet
wait for next tweet
but the problem i am facing is, up to replying and retweeting it's working well, but since it retweeted the received tweet with the same hashtag in the status it repeats the same procedure for the hashtag as before and receives 'duplicate content error'.
is there anything quick solution for this.
require("dotenv").config();
const twit = require("twit");
const fs = require("fs");
const path = require("path");
const { resolve } = require("path");
const twitter = new twit({
consumer_key: process.env.TWITTER_API_KEY,
consumer_secret: process.env.TWITTER_API_SECRET,
access_token: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
console.log("starting bot...");
// function getTweets() {
// const getTweetParams = {
// q: "#birkhe",
// count: 10,
// };
// twitter.get("search/tweets", getTweetParams, (err, data, response) => {
// return console.log(data);
// postTweet();
// });
// }
// var Nepal = ["84.1240", "28.3949"];
var stream = twitter.stream("statuses/filter", {
track: "#topi"
// geo: Nepal
});
console.log("waiting for tweets...");
console.log('----------------------');
stream.on("tweet", async (tweet) => {
console.log("writing received tweets to a file...");
fs.writeFileSync(path.join(__dirname, "tweets.json"), JSON.stringify(tweet));
console.log("writing tweet to a file is completed...");
console.log('----------------------');
const user_name = tweet.user.screen_name;
const name = tweet.user.name;
const tweetId = tweet.id_str;
const receivedTewwtText = tweet.text;
console.log("user Name id:", user_name);
console.log("name id:", name);
console.log("tweetId:", tweetId);
console.log("received text:", receivedTewwtText);
const tweetText = `#${user_name} have a good day!`;
try{
await replyTweet(tweetText);
await retweet(tweetId);
}catch(e){
console.log("the Error is ", e);
}
});
function retweet(id) {
return new Promise((reject, resolve) => {
console.log("posting retweet...");
let postRetweetParams = {
id,
};
twitter.post("statuses/retweet/:id", postRetweetParams, (err, data) => {
if (err) {
console.log("there is an error while posting retweet..");
return reject(err);
}
console.log("retweeted successfully...");
console.log('----------------------');
return resolve(data);
});
});
}
function replyTweet(text) {
return new Promise((resolve, reject) => {
console.log("replting tweet...");
let postTweetParams = {
status: text,
};
twitter.post("statuses/update", postTweetParams, (err, data) => {
if (err) {
console.log("there is an error while replting to a tweet...");
return reject(err);
}
console.log("reply tweet ID is, ", data.id_str);
console.log("replting twit completed...");
console.log('----------------------');
return resolve(data);
});
});
}
starting bot...
waiting for tweets...
----------------------
writing received tweets to a file...
writing tweet to a file is completed...
----------------------
user Name id: LaxmanP71300671
name id: Laxman Pokhrel
tweetId: 1410595913265938445
received text: Hello there #topi
replting tweet...
reply tweet ID is, 1410595936984731648
replting twit completed...
----------------------
posting retweet...
retweeted successfully...
----------------------
the Error is {
created_at: 'Thu Jul 01 13:47:34 +0000 2021',
id: 1410595939673206800,
id_str: '1410595939673206792',
text: 'RT #LaxmanP71300671: Hello there #topi',
truncated: false,
entities: {
hashtags: [ [Object] ],
symbols: [],
user_mentions: [ [Object] ],
urls: []
},
source: 'बिर्खे',
in_reply_to_status_id: null,
in_reply_to_status_id_str: null,
in_reply_to_user_id: null,
in_reply_to_user_id_str: null,
in_reply_to_screen_name: null,
user: {
id: 1407231034958094300,
id_str: '1407231034958094337',
name: 'बिर्खे',
screen_name: 'webtron_birkhe',
location: 'Kathmandu, Nepal',
description: 'म बिर्खे हुँ । म हजुरको सेवामा चौबिसै घण्टा उपलब्ध छु ।',
url: '/*some url*/',
entities: { url: [Object], description: [Object] },
protected: false,
followers_count: 2,
friends_count: 25,
listed_count: 0,
created_at: 'Tue Jun 22 06:57:05 +0000 2021',
favourites_count: 0,
utc_offset: null,
time_zone: null,
geo_enabled: false,
verified: false,
statuses_count: 9,
lang: null,
contributors_enabled: false,
is_translator: false,
is_translation_enabled: false,
profile_background_color: 'F5F8FA',
profile_background_image_url: null,
profile_background_image_url_https: null,
profile_background_tile: false,
profile_image_url: 'http://pbs.twimg.com/profile_images/1407232001204129793/47DUsEQN_normal.jpg',
profile_image_url_https: 'https://pbs.twimg.com/profile_images/1407232001204129793/47DUsEQN_normal.jpg',
profile_banner_url: 'https://pbs.twimg.com/profile_banners/1407231034958094337/1624973880',
profile_link_color: '1DA1F2',
profile_sidebar_border_color: 'C0DEED',
profile_sidebar_fill_color: 'DDEEF6',
profile_text_color: '333333',
profile_use_background_image: true,
has_extended_profile: true,
default_profile: true,
default_profile_image: false,
following: false,
follow_request_sent: false,
notifications: false,
translator_type: 'none',
withheld_in_countries: []
},
geo: null,
coordinates: null,
place: null,
contributors: null,
retweeted_status: {
created_at: 'Thu Jul 01 13:47:27 +0000 2021',
id: 1410595913265938400,
id_str: '1410595913265938445',
text: 'Hello there #topi',
truncated: false,
entities: { hashtags: [Array], symbols: [], user_mentions: [], urls: [] },
source: 'Twitter for Android',
in_reply_to_status_id: null,
in_reply_to_status_id_str: null,
in_reply_to_user_id: null,
in_reply_to_user_id_str: null,
in_reply_to_screen_name: null,
user: {
id: 1348101598157840400,
id_str: '1348101598157840384',
name: 'Laxman Pokhrel',
screen_name: 'LaxmanP71300671',
location: 'Nepal',
description: 'rock🤟',
url: null,
entities: [Object],
protected: false,
followers_count: 2,
friends_count: 18,
listed_count: 0,
created_at: 'Sun Jan 10 02:57:39 +0000 2021',
favourites_count: 4,
utc_offset: null,
time_zone: null,
geo_enabled: false,
verified: false,
statuses_count: 6,
lang: null,
contributors_enabled: false,
is_translator: false,
is_translation_enabled: false,
profile_background_color: 'F5F8FA',
profile_background_image_url: null,
profile_background_image_url_https: null,
profile_background_tile: false,
profile_image_url: 'http://pbs.twimg.com/profile_images/1348102053713727489/xpMY6o-d_normal.jpg',
profile_image_url_https: 'https://pbs.twimg.com/profile_images/1348102053713727489/xpMY6o-d_normal.jpg',
profile_banner_url: 'https://pbs.twimg.com/profile_banners/1348101598157840384/1610247852',
profile_link_color: '1DA1F2',
profile_sidebar_border_color: 'C0DEED',
profile_sidebar_fill_color: 'DDEEF6',
profile_text_color: '333333',
profile_use_background_image: true,
has_extended_profile: true,
default_profile: true,
default_profile_image: false,
following: false,
follow_request_sent: false,
notifications: false,
translator_type: 'none',
withheld_in_countries: []
},
geo: null,
coordinates: null,
place: null,
contributors: null,
is_quote_status: false,
retweet_count: 1,
favorite_count: 0,
favorited: false,
retweeted: true,
lang: 'en'
},
is_quote_status: false,
retweet_count: 1,
favorite_count: 0,
favorited: false,
retweeted: true,
lang: 'en'
}
writing received tweets to a file...
writing tweet to a file is completed...
----------------------
user Name id: webtron_birkhe
name id: बिर्खे
tweetId: 1410595939673206792
received text: RT #LaxmanP71300671: Hello there #topi
replting tweet...
reply tweet ID is, 1410595963484270594
replting twit completed...
----------------------
posting retweet...
there is an error while posting retweet..
please help.

Sequelize and response request GraphQL

I try to have a response on my request GraphQL.
I tried many things but currently I have always the Sequence response, and no the Buckets response (belongs To relation).
I have 2 tables :
Sequence [id | is_active]
Bucket [id | fk_language_id | fk_sequence_id | is_active]
model/sequence.js
'use strict';
module.exports = (sequelize, DataTypes) => {
// define sequence
const Sequence = sequelize.define('sequence', {
is_active: {type: DataTypes.BOOLEAN}
});
Sequence.associate = function (models) {
models.Sequence.hasMany(models.Bucket, {
foreignKey: 'fk_sequence_id'
});
return Sequence;
};
model/bucket.js
'use strict';
module.exports = (sequelize, DataTypes) => {
const Bucket = sequelize.define('bucket', {
code : {type: DataTypes.STRING},
is_active: {type: DataTypes.BOOLEAN}
});
Bucket.associate = function (models) {
models.Bucket.belongsTo(models.Language, {
foreignKey: 'fk_language_id',
});
models.Bucket.belongsTo(models.Sequence, {
foreignKey: 'fk_sequence_id',
});
};
return Bucket;
};
schema.js
# Sequence
type Sequence {
id: Int!,
code: String,
buckets: [Bucket],
is_active: Boolean
}
# Bucket
type Bucket {
id: Int!,
code: String
blocks: [Block]
is_active: Boolean
}
# SequenceInput
input SequenceInput {
buckets: [BucketInput],
is_active: Boolean
}
# BucketInput
input BucketInput {
code: String,
fk_language_id: Int,
fk_sequence_id: Int,
is_active: Boolean
}
type Query {
sequences: [Sequence]
sequence(id: Int): Sequence
buckets: [Bucket]
bucket(id: Int): Bucket
}
type Mutation {
createSequence(input: SequenceInput): Sequence,
}
Request GraphQL
mutation {
createSequence(input: {
is_active: false,
buckets: [
{fk_language_id: 2, code: "Test"}
]
}) {
is_active,
buckets {
id,
code
}
}
}
But I have this result, the Buckets doesn't load :
{
"data": {
"createSequence": {
"is_active": false,
"buckets": []
}
}
}
my mutation :
...
Sequence : {
buckets(sequence) {
return models.Bucket.findAll({
where: {id: sequence.id}
});
},
...
},
...
Mutation : {
createSequence(_, {input}) {
let sequenceId = 0;
// Create Sequence
return models.Sequence.create(input)
.then((sequence) => {
sequenceId = sequence.id;
console.log('sequence created');
// Create Bucket
// Foreach on buckets
return Promise.map(input.buckets, function (bucket) {
bucket.fk_sequence_id = sequenceId;
console.log('bucket created');
return models.Bucket.create(bucket);
})
})
.then(() => {
console.log('load created', sequenceId);
return models.Sequence.findOne({
where : {id: sequenceId},
include: [
{
model: models.Bucket,
where: { fk_sequence_id: sequenceId }
}
]
}).then((response) => {
console.log(response);
return response;
})
});
},
}
The final console.log show many informations...
sequence {
dataValues:
{ id: 416,
is_active: false,
created_at: 2019-03-29T20:33:56.196Z,
updated_at: 2019-03-29T20:33:56.196Z,
buckets: [ [Object] ] },
_previousDataValues:
{ id: 416,
is_active: false,
created_at: 2019-03-29T20:33:56.196Z,
updated_at: 2019-03-29T20:33:56.196Z,
buckets: [ [Object] ] },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: 416 },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'sequences', singular: 'sequence' },
omitNull: false,
createdAt: 'created_at',
updatedAt: 'updated_at',
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [Object],
queryInterface: [Object],
models: [Object],
modelManager: [Object],
connectionManager: [Object],
importCache: [Object],
test: [Object] },
hooks: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: [ [Object] ],
includeNames: [ 'buckets' ],
includeMap: { buckets: [Object] },
includeValidated: true,
attributes: [ 'id', 'is_active', 'created_at', 'updated_at' ],
raw: true },
isNewRecord: false,
buckets:
[ bucket {
dataValues: [Object],
_previousDataValues: [Object],
_changed: {},
_modelOptions: [Object],
_options: [Object],
isNewRecord: false } ] }
Your mutation resolver returns a Promise, which resolves into a Model instance. The promise in question is returned on this line:
return models.Sequence.create(input)
.
As such, the server will wait until that promise is resolved before passing the value forward. Other actions were also waiting on that promise, but they were not the promises returned, so they will not be waited for.
All you have to do is wait for all of your operations to finish before resolving your promise.
createSequence: async (parent, { input }) => {
const sequence = await models.Sequence.create({
is_active: input.is_active
})
if (!input.buckets) return sequence
// You may have to modify your Sequence.buckets resolver to avoid fetching buckets again.
sequence.buckets = await Promise.all(input.buckets.map(bucket => {
// You can avoid these if checks by implementing stricter input types.
// e.g. buckets: [BucketInput!]!
if (!bucket) return null
return models.Bucket.create({
...bucket,
fk_sequence_id: sequence.id
})
}))
return sequence
}
Also, make sure your Sequence.buckets resolver isn't overwriting buckets with faulty data. The resolver you've provided will try to match bucket primary keys with a sequence primary key instead of matching the correct foreign keys with a primary key.
Here's a resolver that will work:
buckets: (parent) => (
parent.buckets // This line may conflict with some of your code and cause problems.
|| models.Bucket.findAll({
where: {fk_sequence_id: parent.id}
})
)

Keystone.js / mongoose virtual fields lean record

I'm trying to produce a lean record for a REST API that include virtual fields.
The official documentation for how to implement virtual fields for Mongoose:
http://mongoosejs.com/docs/guide.html
My model:
var keystone = require('keystone')
, Types = keystone.Field.Types
, list = new keystone.List('Vendors');
list.add({
name : {
first: {type : Types.Text}
, last: {type : Types.Text}
}
});
list.schema.virtual('name.full').get(function() {
return this.name.first + ' ' + this.name.last;
});
list.register();
Now, let's query the model:
var keystone = require('keystone'),
vendors = keystone.list('Vendors');
vendors.model.find()
.exec(function(err, doc){
console.log(doc)
});
Virtual field name.full is not here:
[ { _id: 563acf280f2b2dfd4f59bcf3,
__v: 0,
name: { first: 'Walter', last: 'White' } }]
But if we do this:
vendors.model.find()
.exec(function(err, doc){
console.log(doc.name.full); // "Walter White"
});
Then the virtual shows.
I guess the reason is that when I do a console.log(doc) the Mongoose document.toString() method is invoked which does not include virtuals by default. Fair enough. That's understandable.
To include the virtuals in any of the conversion methods you have to go:
doc.toString({virtuals: true})
doc.toObject({virtuals: true})
doc.toJSON({virtuals: true})
However, this includes keys I don't want for my REST API to pump out to my users:
{ _id: 563acf280f2b2dfd4f59bcf3,
__v: 0,
name: { first: 'Walter', last: 'White', full: 'Walter White' },
_: { name: { last: [Object], first: [Object] } },
list:
List {
options:
{ schema: [Object],
noedit: false,
nocreate: false,
nodelete: false,
autocreate: false,
sortable: false,
hidden: false,
track: false,
inherits: false,
searchFields: '__name__',
defaultSort: '__default__',
defaultColumns: '__name__',
label: 'Vendors' },
key: 'Vendors',
path: 'vendors',
schema:
Schema {
paths: [Object],
subpaths: {},
virtuals: [Object],
nested: [Object],
inherits: {},
callQueue: [],
_indexes: [],
methods: [Object],
statics: {},
tree: [Object],
_requiredpaths: [],
discriminatorMapping: undefined,
_indexedpaths: undefined,
options: [Object] },
schemaFields: [ [Object] ],
uiElements: [ [Object], [Object] ],
underscoreMethods: { name: [Object] },
fields: { 'name.first': [Object], 'name.last': [Object] },
fieldTypes: { text: true },
relationships: {},
mappings:
{ name: null,
createdBy: null,
createdOn: null,
modifiedBy: null,
modifiedOn: null },
model:
{ [Function: model]
base: [Object],
modelName: 'Vendors',
model: [Function: model],
db: [Object],
discriminators: undefined,
schema: [Object],
options: undefined,
collection: [Object] } },
id: '563acf280f2b2dfd4f59bcf3' }
I can always of course just delete the unwanted keys, but this doesn't seem quite right:
vendors.model.findOne()
.exec(function(err, doc){
var c = doc.toObject({virtuals: true});
delete c.list;
delete c._;
console.log(c)
});
This produces what I need:
{ _id: 563acf280f2b2dfd4f59bcf3,
__v: 0,
name: { first: 'Walter', last: 'White', full: 'Walter White' },
id: '563acf280f2b2dfd4f59bcf3' }
Is there not a better way of getting a lean record?
I think you want the select method.. something like this:
vendors.model.findOne()
.select('_id __v name').
.exec(function(err, doc){
console.log(c)
});
Also personally I prefer setting virtuals: true on the schema rather than the document, but depends on use case I guess.
One solution would be to use a module like Lodash (or Underscore) which allows you pick a whitelist of property names:
vendors.model.findOne()
.exec(function(err, doc){
var c = _.pick(doc, ['id', 'name.first', 'name.last', 'name.full']);
console.log(c)
});
Given your use-case of serving this data via REST API, I think explicitly defining a whitelist of property names is safer. You could even define a virtual property on your schema which returns the predefined whitelist:
list.schema.virtual('whitelist').get(function() {
return ['id', 'name.first', 'name.last', 'name.full'];
});
and use it in multiple places, or have different versions of your whitelist, all managed at the model layer.

Model.findOne not returning docs but returning a wrapper object

I have defined a Model with mongoose like this:
var mongoose = require("mongoose")
var Schema = mongoose.Schema
var userObject = Object.create({
alias: String,
email: String,
password: String,
updated: {
type: Date,
default: Date.now
}
})
var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)
module.exports = User
Then I created a user that I can perfectly find through mongo console like this:
db.users.findOne({ email: "coco#coco.com" });
{
"_id" : ObjectId("55e97420d82ebdea3497afc7"),
"password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
"gravatar" : "a4bfba4352aeadf620acb1468337fa49",
"email" : "coco#coco.com",
"alias" : "coco",
"updated" : ISODate("2015-09-04T10:36:16.059Z"),
"apps" : [ ],
"__v" : 0
}
However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:
This piece of code...
// Find the user for which the login queries
var User = require('../models/User')
User.findOne({ email: mail }, function(err, doc) {
if (err) throw err
if (doc) {
console.dir(doc)
if(doc.password == pass) // Passwords won't match
Produces this output from console.dir(doc)...
{ '$__':
{ strictMode: false,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: undefined,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
ownerDocument: undefined,
fullPath: undefined,
emitter: { domain: null, _events: {}, _maxListeners: 0 } },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
apps: [],
updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
alias: 'coco',
email: 'coco#coco.com',
gravatar: 'a4bfba4352aeadf620acb1468337fa49',
password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
_id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
'$__original_validate': { [Function] numAsyncPres: 0 },
validate: [Function: wrappedPointCut],
_pres: { '$__original_validate': [ [Object] ] },
_posts: { '$__original_validate': [] } }
Therefore, passwords won't match because doc.password is undefined.
Why is this caused?
That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.

Resources