DB:Mongo ODM:我使用 Mongoose 作为 ODM。
我正在为我的应用程序编写 document.save() 函数的负面测试。如何在保存文档时模拟或复制错误,以便我可以相应地断言。
const CreateArtist = async (artist) => {
try {
await dbConnect();
const user = await new Artist(artist);
await user.validate();
return await user.save(); // want to test for error on save.
} catch (err) {
throw err;
}
};
Run Code Online (Sandbox Code Playgroud)
我曾尝试更改连接字符串,但出现连接字符串错误错误。我不确定如何在保存时复制错误。
出于某些原因,我继续收到此错误,(请参见随附的屏幕截图).我尝试添加一个_.bindAll(this);甚至尝试升级我的代码以获得最新版本的backbonejs.仍然没有运气.
有人可以帮我吗?

var app = app || {};
(function ($) {
'use strict';
app.EmployeeView = Backbone.View.extend({
el: '#container',
model: app.Employee,
events: {
'click #save' : 'saveEntry'
},
initialize: function(){
console.log('Inside Initialization!');
this.$empName = this.$('#txtEmpName');
this.$department = this.$('#txtDepartment');
this.$designation = this.$('#txtDesignation');
this.listenTo(app.employees, 'add', this.addEmployee);
app.employees.fetch();
console.log('End of Initialization!');
//this.render();
},
render: function () {
console.log('Inside Render!!');
console.log(this.model);
this.$el.html(this.template(this.model.toJSON()));
console.log('Inside End of Render!!');
return this;
},
newAttributes: function(){
return{
empName: this.$empName.val(),
department: this.$department.val(),
designation: this.$designation.val()
};
},
saveEntry: function(){
console.log('Inside …Run Code Online (Sandbox Code Playgroud) 我有一个声明的数组使用Object.assign:
var obj = [{
id: 1,
value: 'abc'
}, {
id: 2,
value: 'xyz'
}];
console.log(Object.assign({}, obj));Run Code Online (Sandbox Code Playgroud)
它正在变换为,
{
'0': { id: 1, value: 'abc' },
'1': { id: 2, value: 'xyz' }
}
Run Code Online (Sandbox Code Playgroud)
不过,我需要有这样的作为,(这里1并2代表id我的对象的属性)
{
'1': {id: 1, value: 'abc'},
'2': {id: 2, value: 'xyz'}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些,但没有运气,有人可以解决一些问题吗?
我在mapkit中有2个引脚,两个都在相同的注释视图下,所以它使得两个引脚都是相同的颜色.如何使引脚具有不同的颜色.我想你好红色和hellox是蓝色的.
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var jmap: MKMapView!
override func viewDidLoad() {
jmap.delegate = self;
let hello = MKPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40, longitude: -73)
jmap.addAnnotation(hello)
let hellox = MKPointAnnotation()
hellox.coordinate = CLLocationCoordinate2D(latitude: 34, longitude: -72)
jmap.addAnnotation(hellox)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKPinAnnotationView()
annotationView.pinTintColor = .blue
return annotationView
}}
Run Code Online (Sandbox Code Playgroud)