我正在尝试使用节点模块shopify-express: https://github.com/Shopify/shopify-express 之所以使用自定义策略来存储商店令牌,是因为由于错误的SQL语句(github上已经存在问题),sqlstrategy无法与postgres一起使用。
在文档中,他们说我需要这样一个类:
class Strategy {
// shop refers to the shop's domain name
getShop({ shop }): Promise<{accessToken: string}>
// shop refers to the shop's domain name
storeShop({ shop, accessToken }): Promise<{accessToken: string}>
}
我参加了以下课程:
class Strategy {
// shop refers to the shop's domain name
// Promise<{accessToken: string}>
getShop(shopInfo) {
const { shop } = shopInfo;
console.log('get shop info', shopInfo)
console.log('check domain', shop)
return db.Shop.find({
where: {
shopify_domain: shop
}
})
.then(foundShop => {
return { accessToken: foundShop.access_token }
})
}
// shop refers to the shop's domain name
// Promise<{accessToken: string}>
storeShop(shopInfo) {
const { shop, accessToken } = shopInfo;
console.log('storing store', 'shop variable', shop, 'access token variable', accessToken);
return db.Shop.find({
where: {
shopify_domain: shop
}
})
.then(foundShop => {
if (foundShop) {
foundShop.access_token = accessToken
return foundShop.save()
.then(foundShop => {
return { accessToken: foundShop.access_token }
})
} else {
return db.Shop.create({ shopify_domain: shop, access_token: accessToken }).then(newShop => {
return { accessToken: newShop.access_token }
})
}
})
}
}
发生的事情是它正确插入但冻结。我相信这是因为我没有兑现诺言吗?我仍然在绕着他们的头,所以不确定我是否做对了。
如果有帮助: 这是storeShop方法上的shopInfo变量:
{
accessToken: 'f444ca7789e9bcfb0fb2dc6026a79ece',
shop: 'jldesigndevstore.myshopify.com'
}
(err, token) => {
if (err) {
console.error(' Error storing shop access token', err);
}
if (request.session) {
request.session.accessToken = accessToken;
request.session.shop = shop;
} else {
console.warn('Session not present on request, please install a session middleware.');
}
afterAuth(request, response);
}
答案 0 :(得分:1)
因此,证明该文档不正确。进入node_module,发现内存策略有所不同。这是使用Sequelize起作用的类:
class SequelizeStrategy {
async getShop({ shop }) {
let foundShop = await db.Shop.find({
where: {
shopify_domain: shop
}
})
.catch(err => {
console.log('get shop error', err)
})
return done(null, {accessToken: foundShop.access_token});
}
async storeShop({ shop, accessToken }, done) {
let storedShop = await db.Shop.find({
where: {
shopify_domain: shop
}
})
.then(foundShop => {
if (foundShop) {
foundShop.access_token = accessToken
foundShop.save()
return foundShop;
} else {
return db.Shop.create({ shopify_domain: shop, access_token: accessToken })
}
})
.catch(err => {
console.log('store shop error happened', err);
return done(err);
})
return done(null, accessToken);
}
}