我正在使用firebase云功能为我的Android应用程序使用'call'函数创建Razorpay的订单id,如下所示。但是我无法上传代码-
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const cors = require('cors')({origin: true});
Object.defineProperty(exports, "__esModule", { value: true });
const Razorpay = require('razorpay')
const instance = new Razorpay({ key_id: 'myKey', key_secret: 'mySecretKey' })
exports.createOrder = functions.https.onCall((data, context) => {
var options = {
amount: 50000, // amount in paisa
currency: "INR",
receipt: "order_rcptid_11"
};
try{
const order = await instance.orders.create(options);
res.send(order);
}catch(e){
console.log(e);
res.status(403).send({error: 'error'});
}
});
错误是-
error Parsing error: Unexpected token instance
events.js:200
throw er; // Unhandled 'error' event
^
这里22表示以下行
const order = await instance.orders.create(options);
有什么问题吗?我不太了解JS和NODE JS。
包。function文件夹中的Json文件
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
包。函数文件夹外的Json文件
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"razorpay": "^2.0.6"
}
}
完整的错误信息
22:29 error Parsing error: Unexpected token instance
✖ 1 problem (1 error, 0 warnings)
events.js:200
throw er; // Unhandled 'error' event
^
Error: spawn npm --prefix "%RESOURCE_DIR%" run lint ENOENT
at notFoundError (C:UsersBhaskarAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulescross-envnode_modulescross-spawnlibenoent.js:6:26)
at verifyENOENT (C:UsersBhaskarAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulescross-envnode_modulescross-spawnlibenoent.js:40:16)
at ChildProcess.cp.emit (C:UsersBhaskarAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulescross-envnode_modulescross-spawnlibenoent.js:27:25)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
Emitted 'error' event on ChildProcess instance at:
at ChildProcess.cp.emit (C:UsersBhaskarAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulescross-envnode_modulescross-spawnlibenoent.js:30:37)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12) {
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn npm --prefix "%RESOURCE_DIR%" run lint',
path: 'npm --prefix "%RESOURCE_DIR%" run lint',
spawnargs: []
}
Error: functions predeploy error: Command terminated with non-zero exit code1
这里22表示以下行
const order = await instance.orders.create(options);
我认为这与await
有关。不知道为什么在错误中不那么清楚,然而,await
可以在异步函数中使用(参考)。所以我们有3个选项要纠正(可能更多,但这里给出3个):
- 使函数异步添加
async
关键字,如下所示:
...
exports.createOrder = functions.https.onCall( async (data, context)=> {
...
try
使用then
代替await
:
...
try{
instance.orders.create(options).then(order => res.send(order));
}catch(e){
...
- 使用回调函数,就像在Razorpay的例子。
我在我这边测试了前2个,它们部署正确。我不知道这是否正常工作,因为这是我第一次看到Razorpay。但是,它部署时没有错误。