无法将云功能部署到 Firebase
Unable to deploy cloud function to Firebase
当我尝试使用 firebase cli 将此功能部署到 Firebase 时,它在 cli 中出错。这个函数的作用是
- 检查给定的号码是否被屏蔽,如果被屏蔽,它会发送响应。
- 如果号码没有被拒绝,它会检查号码是否与其他帐户关联,如果是,它会发送响应
- 如果1和2都为假,则注册用户。
exports.register = functions.https.onRequest((request, response) => {
const db = admin.firestore();
const user: string = request.body['username'];
const phone: number = request.body['phone'];
const password: string = request.body['password'];
return db.collection('rejectedContacts').where('contact', '==', phone).get()
.then(snapShot => {
if (snapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is blocked, please try again with another number`,
result: null
}
);
} else {
return db.collection('users').where('contacts.phone', '==', phone).get()
.then(snapShot => {
if (snapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is already assigned with an accont.
Did you forgot your pasword?`,
result: null
}
);
} else {
return db.collection('users').add(
{
user: user,
password: password,
isBlocked: false,
joiningDate: Date.now(),
phoneVerified: false,
deleted: false,
contacts:
{
phone: phone
}
}
).then((writeResult) => {
return response.json(
{
result: `User with ID: ${writeResult.id} added.`
}
);
});
}
});
}
});
});
我在 cli 中遇到的错误是
=== Deploying to 'functionstest-54bd9'...
i deploying functions
Running command: npm --prefix $RESOURCE_DIR run lint
> functions@ lint /home/me/Documents/TfmFirebase/functions
> tslint -p tslint.json
ERROR: /home/me/Documents/TfmFirebase/functions/src/index.ts[98, 27]: Shadowed name: 'snapShot'
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[140, 9]: Identifier 'phone' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[141, 9]: Identifier 'password' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[145, 9]: Identifier 'queryRef' is never reassigned; use 'const' instead of 'let'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint -p tslint.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/me/.npm/_logs/2018-02-13T17_43_10_045Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
这是日志
0 info it worked if it ends with ok
1 verbose cli [ '/home/me/.nvm/versions/node/v9.5.0/bin/node',
1 verbose cli '/home/me/.nvm/versions/node/v9.5.0/bin/npm',
1 verbose cli '--prefix',
1 verbose cli '/home/me/Documents/TfmFirebase/functions',
1 verbose cli 'run',
1 verbose cli 'lint' ]
2 info using npm@5.6.0
3 info using node@v9.5.0
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /home/me/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/me/Documents/TfmFirebase/functions/node_module$
9 verbose lifecycle functions@~lint: CWD: /home/me/Documents/TfmFirebase/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'tslint -p tslint.json' ]
11 silly lifecycle functions@~lint: Returned: code: 2 signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `tslint -p tslint.json`
13 verbose stack Exit status 2
13 verbose stack at EventEmitter.<anonymous> (/home/me/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack at EventEmitter.emit (events.js:160:13)
13 verbose stack at ChildProcess.<anonymous> (/home/me/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:160:13)
13 verbose stack at maybeClose (internal/child_process.js:943:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid functions@
15 verbose cwd /home/me/Documents/TfmFirebase
16 verbose Linux 4.10.0-42-generic
17 verbose argv "/home/me/.nvm/versions/node/v9.5.0/bin/node" "/home/me/.nvm/versions/node/v9.5.0/bin/npm" "--prefix" "/home/me/Documents/TfmFirebase/functions" "run" "lint"
18 verbose node v9.5.0
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 2
22 error functions@ lint: `tslint -p tslint.json`
22 error Exit status 2
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 2, true ]
但是当我把函数分成三个独立的函数时
(1. 检查被拒绝的联系人,2. 检查号码是否正在使用,3. 注册号码)它有效。位我不想进行三个调用来注册用户并且每个函数调用都在云函数中计数。
谁能告诉我代码有什么问题?
您项目中的 TSLint 预部署挂钩给您一个错误和三个警告:
ERROR: /home/me/Documents/TfmFirebase/functions/src/index.ts[98, 27]: Shadowed name: 'snapShot'
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[140, 9]: Identifier 'phone' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[141, 9]: Identifier 'password' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[145, 9]: Identifier 'queryRef' is never reassigned; use 'const' instead of 'let'.
您不必修复警告,但必须修复错误(或禁用 TSLint 预部署挂钩)。
第一行中的错误是说您将一个变量名与另一个变量名重叠。这有时表示编程错误。该变量是 snapShot
,您使用了它两次,一次嵌套在另一个函数中。您可以通过更改其中一个用途的名称来解决此问题,这样另一个用途就不会被重新定义。
您还可以重组您的承诺,使其不嵌套。
当我尝试使用 firebase cli 将此功能部署到 Firebase 时,它在 cli 中出错。这个函数的作用是
- 检查给定的号码是否被屏蔽,如果被屏蔽,它会发送响应。
- 如果号码没有被拒绝,它会检查号码是否与其他帐户关联,如果是,它会发送响应
- 如果1和2都为假,则注册用户。
exports.register = functions.https.onRequest((request, response) => {
const db = admin.firestore();
const user: string = request.body['username'];
const phone: number = request.body['phone'];
const password: string = request.body['password'];
return db.collection('rejectedContacts').where('contact', '==', phone).get()
.then(snapShot => {
if (snapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is blocked, please try again with another number`,
result: null
}
);
} else {
return db.collection('users').where('contacts.phone', '==', phone).get()
.then(snapShot => {
if (snapShot.size > 0) {
return response.json(
{
status: 0,
message: `Contact, ${phone} is already assigned with an accont.
Did you forgot your pasword?`,
result: null
}
);
} else {
return db.collection('users').add(
{
user: user,
password: password,
isBlocked: false,
joiningDate: Date.now(),
phoneVerified: false,
deleted: false,
contacts:
{
phone: phone
}
}
).then((writeResult) => {
return response.json(
{
result: `User with ID: ${writeResult.id} added.`
}
);
});
}
});
}
});
});
我在 cli 中遇到的错误是
=== Deploying to 'functionstest-54bd9'...
i deploying functions
Running command: npm --prefix $RESOURCE_DIR run lint
> functions@ lint /home/me/Documents/TfmFirebase/functions
> tslint -p tslint.json
ERROR: /home/me/Documents/TfmFirebase/functions/src/index.ts[98, 27]: Shadowed name: 'snapShot'
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[140, 9]: Identifier 'phone' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[141, 9]: Identifier 'password' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[145, 9]: Identifier 'queryRef' is never reassigned; use 'const' instead of 'let'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint -p tslint.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/me/.npm/_logs/2018-02-13T17_43_10_045Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
这是日志
0 info it worked if it ends with ok
1 verbose cli [ '/home/me/.nvm/versions/node/v9.5.0/bin/node',
1 verbose cli '/home/me/.nvm/versions/node/v9.5.0/bin/npm',
1 verbose cli '--prefix',
1 verbose cli '/home/me/Documents/TfmFirebase/functions',
1 verbose cli 'run',
1 verbose cli 'lint' ]
2 info using npm@5.6.0
3 info using node@v9.5.0
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /home/me/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/me/Documents/TfmFirebase/functions/node_module$
9 verbose lifecycle functions@~lint: CWD: /home/me/Documents/TfmFirebase/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'tslint -p tslint.json' ]
11 silly lifecycle functions@~lint: Returned: code: 2 signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `tslint -p tslint.json`
13 verbose stack Exit status 2
13 verbose stack at EventEmitter.<anonymous> (/home/me/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack at EventEmitter.emit (events.js:160:13)
13 verbose stack at ChildProcess.<anonymous> (/home/me/.nvm/versions/node/v9.5.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:160:13)
13 verbose stack at maybeClose (internal/child_process.js:943:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid functions@
15 verbose cwd /home/me/Documents/TfmFirebase
16 verbose Linux 4.10.0-42-generic
17 verbose argv "/home/me/.nvm/versions/node/v9.5.0/bin/node" "/home/me/.nvm/versions/node/v9.5.0/bin/npm" "--prefix" "/home/me/Documents/TfmFirebase/functions" "run" "lint"
18 verbose node v9.5.0
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 2
22 error functions@ lint: `tslint -p tslint.json`
22 error Exit status 2
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 2, true ]
但是当我把函数分成三个独立的函数时 (1. 检查被拒绝的联系人,2. 检查号码是否正在使用,3. 注册号码)它有效。位我不想进行三个调用来注册用户并且每个函数调用都在云函数中计数。
谁能告诉我代码有什么问题?
您项目中的 TSLint 预部署挂钩给您一个错误和三个警告:
ERROR: /home/me/Documents/TfmFirebase/functions/src/index.ts[98, 27]: Shadowed name: 'snapShot'
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[140, 9]: Identifier 'phone' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[141, 9]: Identifier 'password' is never reassigned; use 'const' instead of 'let'.
WARNING: /home/me/Documents/TfmFirebase/functions/src/index.ts[145, 9]: Identifier 'queryRef' is never reassigned; use 'const' instead of 'let'.
您不必修复警告,但必须修复错误(或禁用 TSLint 预部署挂钩)。
第一行中的错误是说您将一个变量名与另一个变量名重叠。这有时表示编程错误。该变量是 snapShot
,您使用了它两次,一次嵌套在另一个函数中。您可以通过更改其中一个用途的名称来解决此问题,这样另一个用途就不会被重新定义。
您还可以重组您的承诺,使其不嵌套。