[英]Code behaving differently in Production vs Locally
I have a Loopback app where I have created some seed scripts to pre-populate the db. 我有一个Loopback应用程序,在其中创建了一些种子脚本来预填充数据库。
Here is the seed 这是种子
const remote = {
"name": "remote",
"email": "remote@ttt.com",
"password": "arglebargle"
}
app.models.AppUser.find({where: {email: 'remoteUser@ttt.com'}})
.then(res => {
if (res.length === 0) {
createUsers(remote, 'remote')
} else {
console.log('remote user already exists')
}
})
This calls createUsers which is below 这将调用下面的createUsers
const app = require('../server')
const Promise = require('bluebird');
module.exports = {
createUsers: (userInfo, roleName) => {
if (!userInfo || !roleName) {
let err = new Error('please give valid user and role names')
console.log(err)
return err
}
console.log(userInfo)
return app.models.AppUser.findOrCreate({where: {'name': userInfo.name}}, userInfo)
.then((instance) => {
return app.models.Role.findOne({where: {name: roleName}})
.then((role) => {
return role.principals.create({
principalType: app.models.RoleMapping.USER,
principalId: instance[0].id //find or create returns an array
})
})
})
.catch((error) => {
return error
})
}
}
The above probably isn't good promise based code. 上面的代码可能不是基于良好承诺的代码。 But this is working fine in other situations so I am not sure if I can blame it.
但这在其他情况下效果很好,所以我不确定是否可以怪罪。
Running the above script creates the 'remote' user and assigns the 'remote' role to it locally, however it does not do anything in production and I just cannot figure out why. 运行上面的脚本会创建“远程”用户并在本地为其分配“远程”角色,但是它在生产中没有任何作用,我只是想不通为什么。
The only other difference I can think of between production and local is that the I am calling them from different locations (the project root is different) 我可以想到的生产和本地之间的唯一其他区别是,我从不同的位置调用它们(项目根目录不同)
I see a couple issues here. 我在这里看到几个问题。 First, in this block:
首先,在此块中:
createUsers: (userInfo, roleName) => {
if (!userInfo || !roleName) {
let err = new Error('please give valid user and role names')
console.log(err)
return err
}
console.log(userInfo)
return app.models.AppUser.findOrCreate
you're returning an Error and a Promise from one function. 您从一个函数返回一个错误和一个承诺。 Then here:
然后在这里:
if (res.length === 0) {
createUsers(remote, 'remote')
} else {
console.log('remote user already exists')
}
you're ignoring the return value altogether from createUsers
您完全忽略了
createUsers
的返回值
I'd stick with promises, as that seems to be the direction you're going, like so: 我会遵守诺言,因为这似乎是您要走的方向,就像这样:
createUsers: (userInfo, roleName) => {
if (!userInfo || !roleName) {
let err = new Error('please give valid user and role names')
console.log(err)
return Promise.reject(err)
}
...
and you must handle every promise, otherwise it will silently fail. 而且您必须履行每一个诺言,否则它将无声地失败。
if (res.length === 0) {
createUsers(remote, 'remote')
.then(result => console.log('got a result'))
.catch(error => console.error('oh no!', error))
} else ...
And also, the first AppUser.find is not being handled if there is an error: 而且,如果出现错误,则不会处理第一个AppUser.find:
app.models.AppUser.find({where: {email: 'remoteUser@ttt.com'}})
.catch(err => console.error('yikes!', err))
.then(res => {
The code you have now will silently swallow any errors that occur in createUsers or that first call. 您现在拥有的代码将以静默方式吞并createUsers或首次调用中发生的任何错误。 So, something like the database being unreachable in production would never present itself.
因此,在生产中无法访问的数据库之类的东西永远不会出现。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.