How to connect to database while making shopify app?

Highlighted
Shopify Partner
26 2 7

I am learning to make shopify app using the official shopify app development tutorial. But I am unable to connect it to a mongodb database. Can anyone help please? This is the tutorial I am following -> https://help.shopify.com/en/api/tutorials/build-a-shopify-app-with-node-and-react

1 Like
Highlighted
Shopify Staff
Shopify Staff
1555 81 282

I'm not much of a JS developer myself, but there is plenty of documentation available to do just that. I was able to find this with a little searching: https://medium.com/javascript-in-plain-english/full-stack-mongodb-react-node-js-express-js-in-one-si...

 

That's not to say you shouldn't post here, I welcome any community input, bit it might be easier to come to an answer by searching existing resources.

 

Cheers.

0 Likes
Highlighted
New Member
4 0 0
Hey Abhinav ,have you got any idea ? I have same question.
0 Likes
Highlighted
Excursionist
21 0 3

For mongodb you'll need a database host, either local, or a DBaaS. Since you are working with node you'll want to use the MongoDB driver to interact with the database. If you want to get up and running super fast with Node and MongoDB you can check out Meteor. It's my go to for getting projects up and running quickly.

 

After you have your db host and your driver connected, it's super simple to store and update the JSON returned from Shopify.

1 Like
Highlighted
New Member
4 0 0
Thanks for reply.I think I am clear now how to get this working.
I have faced another one issue that is "HMAC validation failed". When i removed this server.use(verifyRequest()); It is working fine.l am not sure why it is failed to verify .If you know about this please help me.Thanks
0 Likes
Highlighted
Excursionist
21 0 3

Since you are using Shopify's KOA integration, the only reason HMAC validation should fail is if the request didn't come from Shopify OR the API key/secret are not the same as on Shopify's end.

1 Like
Highlighted
New Member
4 0 0
No the request is coming from Shopify end .After Shopify authorization it show this message .Also if api key is not valid then I don't think it allow me to install this app.
0 Likes
Highlighted
Excursionist
21 0 3

Yes, I'm just saying that this *should* be the only reasons that HMAC validation fails..

 

Looking at the code for the verify_request middleware, it doesn't seem that HMAC validation should come into play. This middleware just verifies that there is a session and the session has an accessToken attached to it, and if so, sets an HTTP cookie.

 

Could you possibly post your code here, or if you have a git repo that I could dig into to try to solve this?

0 Likes
Highlighted
New Member
4 0 0

Hi 

 

I have followed the same step as they have mentioned in the demo. https://developers.shopify.com/tutorials/build-a-shopify-app-with-node-and-react

Here is a route file  (server.js) :-

 

require('isomorphic-fetch');
const Koa = require('koa');
const next = require('next');
const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
const dotenv = require('dotenv');
const { verifyRequest } = require('@shopify/koa-shopify-auth');
const session = require('koa-session');

dotenv.config();

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

const { SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY } = process.env;

app.prepare().then(() => {
const server = new Koa();
server.use(session(server));
server.keys = [SHOPIFY_API_SECRET_KEY];

server.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET_KEY,
scopes: ['read_products'],
afterAuth(ctx) {
const { shop, accessToken } = ctx.session;
ctx.cookies.set('shopOrigin', shop, { httpOnly: false });
ctx.redirect('/');
},
}),
);

server.use(verifyRequest());
server.use(async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;

});

server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
});

0 Likes
Highlighted
Pathfinder
83 4 8

Did you ever get your Shopify app to connect to the MongoDB? 

0 Likes