I am trying to fill my mongodb document with data from unsplash and randomuser api.
const userdata = await axios.get("https://randomuser.me/api/?results=51");
const imagedat = await axios.get(
"https://api.unsplash.com/photos/random/?count=51&client_id=GWDzPpjHk743C2QnVBRxu8PtmOI3npF5sePZZ7o0pg4"
);
I call both apis for 51 results but after 22 results the code under catch gets displayed and only 22 documents get created
how to store all 51 results
const seeddata = async () => {
const userdata = await axios.get("https://randomuser.me/api/?results=51");
const imagedat = await axios.get(
"https://api.unsplash.com/photos/random/?count=51&client_id=GWDzPpjHk743C2QnVBRxu8PtmOI3npF5sePZZ7o0pg4"
);
try {
await MemoModel.deleteMany({});
const userarr = await userdata.data;
const imagedata = await imagedat.data;
for (let i = 0; i < 50; i++) {
const data = new MemoModel({
name: {
firstname: `${userarr.results[i].name.first}`,
lastname: `${userarr.results[i].name.last}`,
username: `${userarr.results[i].login.username}`,
},
about: {
user: `${userarr.results[i].gender} aged ${userarr.results[i].dob.age}. Rotting here for ${userarr.results[i].registered.age} `,
},
location: {
country: `${
countryarr[Math.floor(Math.random() * countryarr.length)]
}`,
state: `${userarr.results[i].location.state}`,
city: `${userarr.results[i].location.city}`,
address: `${userarr.results[i].location.street.name} ,${userarr.results[i].location.street.number}`,
zipcode: `${userarr.results[i].location.postcode}`,
},
email: {
user: `${userarr.results[i].email}`,
},
image: {
dp: `${userarr.results[i].picture.large}`,
coverimage: "https://source.unsplash.com/random/?mountains",
},
posts: {
postno: i,
posttitle: `${imagedata[i].description}`,
postcontent: `${imagedata[i].urls.regular}`,
postlikesno: imagedata[i].likes,
postcommentno: imagedata[i].width,
},
});
await data.save();
}
} catch {
console.log("catastrophic Failure");
}
};
seeddata().then(() => {
mongoose.connection.close();
});
Can you add (err) after your catch and console it instead of "catastrophic error"? It will give us the whole error that break your code:
catch(err) {
console.error(err)
}
Related
I'm new to mongooseDB and i'm trying to insert data from this API to the database, it creates the Collection but not the documents, any idea what could i be doing wrong?
import fetch from 'node-fetch';
import mongoose, { mongo } from 'mongoose';
mongoose.connect("mongodb://localhost/highscore");
const postSchema = new mongoose.Schema({
position: {
type: Number,
required: true
},
id: {
type: Number,
required: true
},
score: {
type: Number,
required: true
},
});
const Post = mongoose.model('Players', postSchema);
async function getPlayers() {
const getPlayers = await fetch("http://localhost:3008/api/highscore/players");
const response = await getPlayers.json();
for (let i = 0; i < response.lenght; i++) {
const post = new Post({
position: response[i]['position'],
id: response[i]['id'],
score: response[i]['score'],
});
post.save()
}
}
getPlayers();```
MongoDB provides Api to insert many documents at the same time, example below.
// check if your mongoDb is connected
mongoose.connect(URI).then(err => {
if(err) console.error({err});
else console.info("Database Connected");
});
async function getPlayers() {
const getPlayers = await fetch("http://localhost:3008/api/highscore/players");
try{
const response = await getPlayers.json();
// And then check if you are getting players
console.log({response});
const posts = response.map((player) => ({
position: player['position'],
id: player['id'],
score: player['score'],
}))
// it's not required to await it, just to be sure it did insert
await Post.insertMany(posts);
}catch(error){
console.log({error})
}
}
This line:
for (let i = 0; i < response.lenght; i++) {
response.length
now your array is empty so the save never happens
EDIT
Are you sure about the imports?
Following code works:
//const fetch = require('fetch');
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/highscore");
const postSchema = new mongoose.Schema({
position: {
type: Number,
required: true
},
id: {
type: Number,
required: true
},
score: {
type: Number,
required: true
},
});
const Post = mongoose.model('Players', postSchema);
async function getPlayers() {
/*
const getPlayers = await fetch("http://localhost:3008/api/highscore/players");
const response = await getPlayers.json();
for (let i = 0; i < response.lenght; i++) {
*/
const post = new Post({
position: '12',
id: '15',
score: '300',
});
post.save()
} //} getPlayers();
Solution:
import fetch from 'node-fetch';
import mongoose, { mongo } from 'mongoose';
mongoose.connect("mongodb://127.0.0.1:27017/highscore");
const postSchema = new mongoose.Schema({
position: {
type: Number,
required: true
},
id: {
type: Number,
required: true
},
score: {
type: Number,
required: true
},
});
const Post = mongoose.model('Players', postSchema);
async function getPosts() {
const getPlayers = await fetch("http://localhost:3008/api/highscore/players");
const response = await getPlayers.json();
for( let i = 0;i < response.players.length; i++){
const post = new Post({
position: response.players[i]['position'],
id: response.players[i]['id'],
score: response.players[i]['score'],
});
post.save();
}
}
getPosts();
By default mongodb runs on localhost: 27017
Check your mongodb connection url
mongoose.connect('mongodb://localhost:27017/highscore')
I am having a hard time understanding serverTimestamp in firestore.
When I save a document in database in a firebase function using Fieldvalue.serverTimestamp() or in a javascript client code using serverTimestamp() it sometimes doesn't save the same thing in the database.
See screenshots below :
Sometime I get an object with {nanoseconds: xxx, seconds: xxx} and sometimes I get a timestamp formatted date...
The problem is when I try to query my orders using query(collectionRefOrders, orderBy('createdAt', 'desc'), limit(10)).
The orders with the object appears before the others ones even if they are created after...
Any clue why this happens ? What am I doing wrong ?
Thanks a lot.
EDIT :
Here is the code I use to add documents in the my firebase function (it is a request function I call in a website) :
const { getFirestore, FieldValue } = require('firebase-admin/firestore');
const firebaseDB = getFirestore();
exports.createOrderFromTunnel = functions.region('europe-west3')
.runWith({
timeoutSeconds: 10,
memory: "4GB",
})
.https
.onRequest(async (req, res) => {
cors(req, res, async () => {
try {
const { apiKey } = req.body;
const project = await getProjectFromApiKey(apiKey);
if (!project) {
return res.json({
success: false,
error: 'Unauthorized: invalid or missing api key'
});
}
const contactData = {
address: {},
createdAt: FieldValue.serverTimestamp()
};
const orderData = {
accounting: {
totalHT: 0,
totalTTC: 0,
totalTVA: 0,
},
createdAt: FieldValue.serverTimestamp(),
status: 'NEW',
};
const refProject = firebaseDB
.collection('projects')
.doc(project.id);
const colOrder = firebaseDB.collection(`projects/${project.id}/orders`)
const refOrder = colOrder.doc();
const colContact = firebaseDB.collection(`projects/${project.id}/contacts`)
const refContact = colContact.doc();
await firebaseDB.runTransaction(async transaction => {
const snapProject = await transaction.get(refProject);
const dataProject = snapProject.data();
const sequenceContact = dataProject.sequenceContact;
const sequenceOrder = dataProject.sequenceOrder;
contactData.sequence = sequenceContact;
orderData.sequenceNumber = sequenceOrder;
await transaction.set(refContact, contactData);
orderData.customer.id = refContact.id;
orderData.customer.sequence = sequenceContact;
await transaction.set(refOrder, orderData);
await transaction.update(refProject, {
sequenceContact: sequenceContact + 1,
sequenceOrder: sequenceOrder + 1,
totalContacts: dataProject.totalContacts + 1,
totalOrders: dataProject.totalOrders + 1,
});
return refOrder.id;
});
return res.json({
success: true
});
} catch (err) {
functions.logger.error(err);
return res.json({
success: false,
err
});
}
});
});
Here is the code I use to add documents in my client code (it is a web app in javascript) :
const createOrder = async (projectId) => {
try {
const orderData = {
accounting: {
totalHT: 0,
totalTTC: 0,
totalTVA: 0,
},
createdAt: serverTimestamp(),
status: 'NEW',
surface: 0,
};
const refProject = doc(firebaseDB, 'projects', projectId);
const colOrder = collection(firebaseDB, `projects/${projectId}/orders`)
const refOrder = doc(colOrder);
return await runTransaction(firebaseDB, async (transaction) => {
const snapProject = await transaction.get(refProject);
if (!snapProject.exists()) {
throw "Document does not exist!";
}
const dataProject = snapProject.data();
const sequence = dataProject.sequenceOrder;
orderData.sequenceNumber = sequence;
transaction.set(refOrder, orderData);
transaction.update(refProject, { sequenceOrder: sequence + 1, totalOrders: dataProject.totalOrders + 1 });
return refOrder.id;
});
} catch (e) {
console.error(e);
return null;
}
};
I'm running into some difficulties iterating through a list of documents. I'm working on an app for distributing season tickets amongst friends and I've created a document store in mongodb. When I post the teamId and number of tickets (groups) it creates a document with the below structure.
{
draftOwner: '',
draftState: 'active',
draftCreate: 1659240148635,
draftGroups: 2,
homeTeam: '',
homeId: 17,
draftInvites: '',
homeSchedule: [
{
date: '2022-10-14T23:30:00Z',
home_id: 17,
home_name: 'Detroit Red Wings',
away_id: 8,
away_name: 'Montréal Canadiens',
ticketName_0: '',
ticketOwner_0: '',
ticketName_1: '',
ticketOwner_1: ''
},
{
date: '2022-10-17T23:30:00Z',
home_id: 17,
home_name: 'Detroit Red Wings',
away_id: 26,
away_name: 'Los Angeles Kings',
ticketName_0: '',
ticketOwner_0: '',
ticketName_1: '',
ticketOwner_1: ''
},
{
date: '2022-10-23T21:00:00Z',
home_id: 17,
home_name: 'Detroit Red Wings',
away_id: 24,
away_name: 'Anaheim Ducks',
ticketName_0: '',
ticketOwner_0: '',
ticketName_1: '',
ticketOwner_1: ''
}, ... ]
Example of my post method that creates documents for reference.
const createDraft = (req, res, next) => {
const teamId = parseInt(req.query.id);
const draftGroups = parseInt(req.query.groups);
const url = nhlScheduleAPI + teamId.toString() + season;
let settings = { method: "Get"};
fetch(url, settings)
.then(res => res.json())
.then((json) => {
let games = json['dates'];
let draftSchedule = [];
for (let i = 0; i < games.length; i++) {
let row = {};
if (games[i]['games'][0]['teams']['home']['team']['id'] === teamId) {
Object.assign(row, {date: games[i]['games'][0]['gameDate']});
Object.assign(row, {home_id: games[i]['games'][0]['teams']['home']['team']['id']});
Object.assign(row, {home_name: games[i]['games'][0]['teams']['home']['team']['name']});
Object.assign(row, {away_id: games[i]['games'][0]['teams']['away']['team']['id']});
Object.assign(row, {away_name: games[i]['games'][0]['teams']['away']['team']['name']});
for (let n = 0; n < draftGroups; n++) {
let ticketName = "ticketName_" + n.toString();
let ticketOwner = "ticketOwner_" + n.toString();
Object.assign(row, {[ticketName]: ""})
Object.assign(row, {[ticketOwner]: ""})
}
draftSchedule.push(row);
}
}
let newDraftObj = new Object({ draftOwner: "", draftState: "active", draftCreate: Date.now(),
draftGroups: draftGroups, homeTeam: "", homeId: teamId, draftInvites: "", homeSchedule: draftSchedule });
const client = new MongoClient(uri);
async function run() {
try {
const database = client.db("ticketdrafterDB");
const drafts = database.collection("drafts");
const result = await drafts.insertOne(newDraftObj);
console.log(result);
console.log(newDraftObj);
} finally {
await client.close();
}
}
run().catch(console.dir);
res.send(newDraftObj)
})
};
So now what I am running into issues with is iterating through the list of the currently {draftState: active} drafts. I'm trying to show a page that just has a table output of each object showing [draftCreate, homeTeam, draftOwner]. Here is what I have created so far and I'm getting console output, so the objects are being retrieved but I just can't display them to the client for some reason.
controller/home.js
const homeView = (req, res, next) => {
const client = new MongoClient(uri);
async function run() {
try {
const database = client.db("ticketdrafterDB");
const collection = database.collection("drafts");
const query = { draftState: "active" };
const options = { projection: { homeSchedule: 1 }};
const drafts = collection.find(query, options);
if ((await drafts.count()) === 0) {
console.log("No documents found!")
}
res.render('index', {drafts: drafts});
} finally {
await client.close();
}
}
run().catch(console.dir);
index.pug
each draft in drafts
li.list-group-item
p #{draft.draftState}
Would really appreciate any guidance here, thanks in advance!
try turning the drafts cursor into an array with method toArray:
const results = await drafts.toArray();
res.render('index', {drafts: results});
I have these data:
orders = {
firstName: "Jennie",
lastName: "asdasda",
address: "US",
order: [
{ product: "Tumbler - 500- ML ", variation: [{ qty: 12, color: "red" }] },
{ product: "Shirt - M ", variation: [{ qty: 14, color: "green" }] }
],
instructions: "asdasdad",
contact: "182738123"
};
Saving this in firestore would look like this:
How do I save this in firestore where the fields of firstName, lastName, address, contact and instructions is not inside the orders map?
I kind of wanted to save it like this:
This is how I saved it in Firestore:
const Save = () => {
const { state } = useStateMachine(updateAction);
const orders = state.yourDetails;
const handleAdd = async () => {
try {
const docRef = await addDoc(collection(db, "orders"), { orders });
console.log("Document written with ID: ", docRef.id);
} catch (err) {
console.log(err);
}
};
return (
<div>
Results
<pre>{JSON.stringify(state.yourDetails, null, 2)}</pre>
<button onClick={handleAdd}>Add </button>
</div>
);
};
export default Save;
remove the curly bracket from { orders }:
const Save = () => {
const { state } = useStateMachine(updateAction);
const orders = state.yourDetails;
const handleAdd = async () => {
try {
const docRef = await addDoc(collection(db, "orders"), orders);
console.log("Document written with ID: ", docRef.id);
} catch (err) {
console.log(err);
}
};
return (
<div>
Results
<pre>{JSON.stringify(state.yourDetails, null, 2)}</pre>
<button onClick={handleAdd}>Add </button>
</div>
);
};
export default Save;
This is the code I tried in gatsby-node.js, using gatsby develop to interface with graphql... I'm trying to source data from a blockchain indexer to display on my website.
const fetch = require('node-fetch');
const NODE_TYPE = 'objkt';
exports.sourceNodes = async ({ actions, createContentDigest, createNodeId }) => {
const { createNode } = actions;
const response = await fetch('https://staging.api.tzkt.io/v1/bigmaps/523/keys?value.issuer=tz1V9ZviaGUWZjGx4U7cGYFEyUGyqpFnVGXx&active=true');
const json = await response.json();
const { results = [] } = json;
const objkt = await Promise.all(results.map(async result => {
const { url } = result;
const objResponse = await fetch(url);
return await objResponse.json();
}));
objkt.forEach((node, index) => {
createNode({
...node,
id: createNodeId(`${NODE_TYPE}-${node.id}`),
parent: null,
children: null,
internal: {
type: NODE_TYPE,
content: JSON.stringify(node),
contentDigest: createContentDigest(node)
}
});
});
};
creates error:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found \"}\".",
"locations": [
{
"line": 4,
"column": 3
}
],
"stack": [
"GraphQLError: Syntax Error: Expected Name, found \"}\".",
data I'm trying to source
I'm very lost as to why this error happens...
SOLUTION:
const fetch = require("node-fetch")
const NODE_TYPE = `objkt`
exports.sourceNodes = async ({
actions,
createContentDigest,
createNodeId,
}) => {
const { createNode } = actions
const response = await fetch(
"https://staging.api.tzkt.io/v1/bigmaps/523/keys?value.issuer=tz1V9ZviaGUWZjGx4U7cGYFEyUGyqpFnVGXx&active=true"
)
const objkt = await response.json()
objkt.forEach((node, index) => {
createNode({
...node,
id: createNodeId(`${NODE_TYPE}-${node.id}`),
parent: null,
children: [],
internal: {
type: NODE_TYPE,
content: JSON.stringify(node),
contentDigest: createContentDigest(node),
},
})
})
}
exports.onPreInit = () => console.log("Loaded gatsby-node")
GraphQL code:
query MyQuery {
objkt {
value {
issuer
objkt_id
objkt_amount
xtz_per_objkt
}
internal {
content
contentDigest
}
}
}