我有一个 NestJS 应用程序,在我的处理程序文件中我导入猫鼬来创建连接:
import * as mongoose from 'mongoose';
let conn = null;
const uri = process.env.MONGO_URI;
mongoose.connection.on('connected', () => {
// log something
});
然后,在我的处理函数中:
export const handler: Handler = async (event: any, context: Context) => {
if (conn == null) {
conn = await mongoose.connect(uri, {
bufferCommands: false, // Disable mongoose buffering
bufferMaxEntries: 0, // and MongoDB driver buffering
useNewUrlParser: true,
});
}
context.callbackWaitsForEmptyEventLoop = false;
现在我已经在没有猫鼬的情况下完全部署并测试了我的应用程序,并且一切正常。
相关无服务器配置:
plugins:
- serverless-plugin-typescript
- serverless-plugin-optimize
- serverless-offline
package:
individually: true
我添加了一个 mongoose 层,在 zip 中我有一个名为“nodejs”的文件夹,其中有 package.json 和“node_modules”文件夹,其中包含 npm 安装的所有依赖项。
在此之前我使用过层,并且没有以相同的方式安装问题。
但是,在尝试任何端点时,我仍然在控制台上得到这个:
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module './drivers/node-mongodb-native/connection'",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module './drivers/node-mongodb-native/connection'",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:36:30)",
" at Module._compile (internal/modules/cjs/loader.js:776:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)",
" at Module.load (internal/modules/cjs/loader.js:653:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)",
" at startup (internal/bootstrap/node.js:283:19)",
" at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)"
]
}