我用NodeJS / Mongoose创建了一个CRUD,将文件拆分为MVC样式。在下面显示的路由示例中,执行 Dim fnt As System.Drawing.Font, size_M As Drawing.Size, size_i As Drawing.Size, size_HTab As Drawing.Size, isMonospace As Boolean
Dim ifc = New Drawing.Text.InstalledFontCollection
Dim bm As Drawing.Bitmap = New Drawing.Bitmap(640, 64), gr = Drawing.Graphics.FromImage(bm)
Dim tf As Windows.Media.Typeface, gtf As Windows.Media.GlyphTypeface = Nothing, ok As Boolean, gtfName = ""
For Each item In ifc.Families
'TestContext_WriteTimedLine($"N={item.Name}.")
fnt = New Drawing.Font(item.Name, 24.0)
Assert.IsNotNull(fnt)
tf = New Windows.Media.Typeface(item.Name)
Assert.IsNotNull(tf, $"item.Name={item.Name}")
size_M = System.Windows.Forms.TextRenderer.MeasureText("M", fnt)
size_i = System.Windows.Forms.TextRenderer.MeasureText("i", fnt)
size_HTab = System.Windows.Forms.TextRenderer.MeasureText(ChrW(&H2409), fnt)
isMonospace = size_M.Width = size_i.Width
Assert.AreEqual(size_M.Height, size_i.Height, $"fnt={fnt.Name}")
If isMonospace Then
gtfName = "-"
ok = tf.TryGetGlyphTypeface(gtf)
If ok Then
Assert.AreEqual(True, ok, $"item.Name={item.Name}")
Assert.IsNotNull(gtf, $"item.Name={item.Name}")
gtfName = $"{gtf.FamilyNames(Globalization.CultureInfo.CurrentUICulture)}"
Assert.AreEqual(True, gtf.CharacterToGlyphMap().ContainsKey(AscW("M")), $"item.Name={item.Name}")
Assert.AreEqual(True, gtf.CharacterToGlyphMap().ContainsKey(AscW("i")), $"item.Name={item.Name}")
Dim t = 0, nMin = &HFFFF, nMax = 0
For n = 0 To &HFFFF
If gtf.CharacterToGlyphMap().ContainsKey(n) Then
If n < nMin Then nMin = n
If n > nMax Then nMax = n
t += 1
End If
Next
gtfName &= $",[x{nMin:X}-x{nMax:X}]#{t}"
ok = gtf.CharacterToGlyphMap().ContainsKey(AscW(ChrW(&H2409)))
If ok Then
gtfName &= ",U+2409"
End If
ok = gtf.CharacterToGlyphMap().ContainsKey(AscW(ChrW(&H2026)))
If ok Then
gtfName &= ",U+2026"
End If
End If
Debug.WriteLine($"{IIf(isMonospace, "*M*", "")} N={fnt.Name}, gtf={gtfName}.")
gr.Clear(Drawing.Color.White)
gr.DrawString($"Mi{ChrW(&H2409)} {fnt.Name}", fnt, New Drawing.SolidBrush(Drawing.Color.Black), 10, 10)
bm.Save($"{fnt.Name}_MiHT.bmp")
End If
Next
例程时,有必要等待其处理,然后将用户重定向到一条路由或另一条路由。我想使用Bluebird等待处理。我需要一些帮助来实现例程。
Index.js -------------------------------------------- ---
retrieveOne
controller.js ------------------------------------------ >
const myCRUD = require('./api/controllers/controller')
router.post('/login', function(req, res, next) {
// HOW TO IMPLEMENT BLUEBIRD HERE?
// How to wait for the "retrieveOne" process and a then do a "if" test (below)?
let ret = myCRUD.retrieveOne({ name: "abc test" });
if(!ret) {
res.redirect('/success')
} else {
res.redirect('/error')
}
})
注意:我已经在S.O.中找到了一些带有和不带有Bluebird的示例,但是我无法使它正常工作: 示例:1,2,3,4,5
答案 0 :(得分:2)
您真的不需要使用Bluebird,因为猫鼬可以与本地Promises很好地工作
在您的控制器中:
const mongoose = require('mongoose');
const Schema = require('./schema-user');
const Model = mongoose.model('users', Schema);
const CRUD = {
retrieveOne: function(query) {
// findOne() returns a Promise ()
// https://mongoosejs.com/docs/promises.html
return Model.findOne(query);
}
}
module.exports = CRUD;
现在,您可以将路由处理程序设为async
函数,而仅await
个承诺
const myCRUD = require('./api/controllers/controller')
router.post('/login', async function(req, res, next) {
let ret = await myCRUD.retrieveOne({ name: "abc test"});
if(!ret) {
res.redirect('/success')
} else {
res.redirect('/error')
}
})
答案 1 :(得分:1)
使用async
和await
const myCRUD = require('./api/controllers/controller')
const Promise = require('bluebird');
router.post('/login', async function(req, res, next) {
// HOW TO IMPLEMENT BLUEBIRD HERE?
// How to wait for the "retrieveOne" process and a then do a "if" test (below)?
let ret = await myCRUD.retrieveOne('{ name: "abc test"');
if(!ret) {
res.redirect('/success')
} else {
res.redirect('/error')
}
})
// controller.js ------------------------------------------
const mongoose = require('mongoose');
const Schema = require('./schema-user');
const Model = mongoose.model('users', Schema);
const CRUD = {
retrieveOne: function(query) {
return new Promise((resolve, reject) => {
Model.findOne(query, function(err, result) {
if (err) reject(err);
resolve(result);
});
});
}
}
module.exports = CRUD;