mongodb
尝试从将代码连接到数据库的网站运行该函数时发生错误。
const MongoClient = require('mongodb')
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
Run Code Online (Sandbox Code Playgroud)
错误是:
client.connect(err => {
^
TypeError: client.connect is not a function
Run Code Online (Sandbox Code Playgroud)
我已经mongodb
安装了 vianpm
和 uri 定义为他们给出的字符串。我还需要什么吗?
在以下代码中,出现此错误:
TypeError [ERR_INVALID_ARG_TYPE]:“原始”参数必须为Function类型。接收类型未定义
const sqlite3 = require('sqlite3').verbose();
const util = require('util');
async function getDB() {
return new Promise(function(resolve, reject) {
let db = new sqlite3.Database('./project.db', (err) => {
if (err) {
console.error(err.message);
reject(err)
} else {
console.log('Connected to the project database.');
resolve(db)
}
});
return db
});
}
try {
// run these statements once to set up the db
let db = getDB();
db.run(`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`);
db.run(`INSERT INTO services(id, service, date) VALUES …
Run Code Online (Sandbox Code Playgroud) 当我尝试上传图像并通过邮递员保存在 public/upload_files 文件夹中时,它显示此错误
节点 -v v10.15.3
npm -v 6.9.0
“错误:ENOENT:没有这样的文件或目录”
这是我的代码
const express = require('express');
const router = express.Router();
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null,'./public/uploaded_files');
},
filename: function(req, file, cb) {
cb(null,new Date().toISOString() + file.originalname);
}
});
const upload = multer({storage:storage});
router.post('/', upload.single('file'), (req,res,next) => {
console.log(req.file);
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
我只是想将图像保存在以下文件夹 public/upload_files
我想在我的express(nodejs)中实现多语言但是我无法理解为什么我的ejs不理解“__”下划线。
应用程序.js
var i18n = require('./i18n');
app.use(i18n);
Run Code Online (Sandbox Code Playgroud)
i18n.js
var i18n = require('i18n');
i18n.configure({
locales:['fr', 'en'],
directory: __dirname + '/locales',
defaultLocale: 'en',
cookie: 'lang'
});
module.exports = function(req, res, next) {
i18n.init(req, res);
res.locals.__ = res.__;
var current_locale = i18n.getLocale();
return next();
};
Run Code Online (Sandbox Code Playgroud)
路由器.js
console.log(res.__('hello')); // print ok
console.log(res.__('helloWithHTML')); // print ok
req.app.render('index', context, function(err, html) {
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'});
res.end(html);
});
Run Code Online (Sandbox Code Playgroud)
/locales/en.json
{
"hello": "Hello.",
"helloWithHTML": "helloWithHTML."
}
Run Code Online (Sandbox Code Playgroud)
索引.ejs
<%= __("hello")%>
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
__ is not defined at eval (eval at compile …
Run Code Online (Sandbox Code Playgroud) 当我尝试使用“请求”访问外部 API 并在 AWS Lambda 中获取响应时,我不知道如何正确放置“返回”代码来返回响应。
\n\nNodeJs 8.10
\n\nvar request = require(\'request\');\n\nmodule.exports.sendcode = async (event) => {\n\n let options = {\n url: \'https://api.netease.im/sms/sendcode.action?\' + content, \n method: \'POST\'\n };\n\n return await request(options, function (error, response, body) {\n console.log(body); \n return {\n statusCode: 200,\n body: JSON.stringify({\n message: body,\n input: event,\n }),\n };\n });\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n当我在无服务器框架中运行此代码时,我得到一个空响应,主体中没有任何内容,实际上它至少应该具有“输入”属性。
\n\n但 console.log 已经记录了 API 的实际响应。
\n\n看起来我的“返回”代码根本没有执行。\n\xef\xbc\x88如果我删除 async 和await,那么程序将挂起直到超时)
\n\n任何人都可以帮助如何修改此代码以使其工作吗?
\n